我正在我的应用程序中实施电话验证。用户将输入 4 个值。这就是我需要的样子:
我怎样才能做这个布局?
LinearLayout
您可以使用with 属性创建此布局android:orientation="horizontal"
。EditText
在里面添加 4LinearLayout
并android:layout_weight="1"
为每个添加属性EditText
for equal width
。
这是您想要的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="horizontal"
android:weightSum="4">
<EditText
android:id="@+id/edit_digit_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"/>
<EditText
android:id="@+id/edit_digit_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"/>
<EditText
android:id="@+id/edit_digit_three"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"/>
<EditText
android:id="@+id/edit_digit_four"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"/>
</LinearLayout>
<Button
android:id="@+id/button_verify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="verify"
android:enabled="false"/>
</LinearLayout>
输出:
#. 如果您想根据 来move
移动光标programmatically
和enable/disable
验证按钮input
,请在您的活动中添加以下代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class PinVerificationActivity extends AppCompatActivity {
EditText inputDigitOne;
EditText inputDigitTwo;
EditText inputDigitThree;
EditText inputDigitFour;
Button buttonVerify;
int digitCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pin_verification);
inputDigitOne = (EditText) findViewById(R.id.edit_digit_one);
inputDigitTwo = (EditText) findViewById(R.id.edit_digit_two);
inputDigitThree = (EditText) findViewById(R.id.edit_digit_three);
inputDigitFour = (EditText) findViewById(R.id.edit_digit_four);
buttonVerify = (Button) findViewById(R.id.button_verify);
inputDigitOne.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
if (count == 0) {
digitCount--;
} else {
digitCount++;
inputDigitOne.clearFocus();
// Move cursor to second input field
inputDigitTwo.requestFocus();
inputDigitTwo.setCursorVisible(true);
}
toggleVerifyButton();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
inputDigitTwo.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
if (count == 0) {
digitCount--;
} else {
digitCount++;
inputDigitTwo.clearFocus();
// Move cursor to third input field
inputDigitThree.requestFocus();
inputDigitThree.setCursorVisible(true);
}
toggleVerifyButton();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
inputDigitThree.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
if (count == 0) {
digitCount--;
} else {
digitCount++;
inputDigitThree.clearFocus();
// Move cursor to forth input field
inputDigitFour.requestFocus();
inputDigitFour.setCursorVisible(true);
}
toggleVerifyButton();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
inputDigitFour.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
if (count == 0)
digitCount--;
else
digitCount++;
toggleVerifyButton();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
// Verify
buttonVerify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringBuilder strPIN = new StringBuilder();
strPIN.append(inputDigitOne.getText().toString());
strPIN.append(inputDigitTwo.getText().toString());
strPIN.append(inputDigitThree.getText().toString());
strPIN.append(inputDigitFour.getText().toString());
// Now verify PIN (Here I have used dummy PIN 1234 for verification)
if (strPIN.toString().equals("1234")) {
// Do something...
Toast.makeText(getApplicationContext(), "PIN Matched!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Incorrect PIN!", Toast.LENGTH_SHORT).show();
}
}
});
}
// Required to enable/disable VERIFY button
private void toggleVerifyButton() {
if (digitCount == 4)
buttonVerify.setEnabled(true);
else
buttonVerify.setEnabled(false);
}
}
输出:
希望这会有所帮助~
试试这个创建这样的布局文件
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
app:cardElevation="10dp"
app:cardUseCompatPadding="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Verify Number"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Enter Code"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="4">
<EditText
android:id="@+id/edDigit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="1"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1" />
<EditText
android:id="@+id/edDigit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="2"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1" />
<EditText
android:id="@+id/edDigit3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="3"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1" />
<EditText
android:id="@+id/edDigit4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="4"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1" />
</LinearLayout>
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="Submit"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
爪哇代码
public class MainActivity extends AppCompatActivity {
EditText edDigit1, edDigit2, edDigit3, edDigit4;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestSmsPermission();
edDigit1 = (EditText) findViewById(R.id.edDigit1);
edDigit2 = (EditText) findViewById(R.id.edDigit2);
edDigit3 = (EditText) findViewById(R.id.edDigit3);
edDigit4 = (EditText) findViewById(R.id.edDigit4);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
edDigit1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (edDigit1.getText().toString().equals("")) {
edDigit1.requestFocus();
} else {
edDigit2.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
edDigit2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (edDigit2.getText().toString().equals("")) {
edDigit2.requestFocus();
} else {
edDigit3.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
edDigit3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (edDigit3.getText().toString().equals("")) {
edDigit3.requestFocus();
} else {
edDigit4.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edDigit1.getText().toString().equals("") || edDigit2.getText().toString().equals("") ||
edDigit3.getText().toString().equals("") || edDigit4.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, "Please fill all digits", Toast.LENGTH_SHORT).show();
} else {
Intent i = new Intent(MainActivity.this, MyProfile.class);
startActivity(i);
Toast.makeText(MainActivity.this, "Jalsaa Karroooooooooooo", Toast.LENGTH_SHORT).show();
}
}
});
}
private void requestSmsPermission() {
String permission = Manifest.permission.READ_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
}
}
问我以防有任何疑问