0
A
回答
1
您可以使用LinearLayout
與屬性android:orientation="horizontal"
此佈局。在LinearLayout
的內部添加4 EditText
,並且還爲每個EditText
添加屬性android:layout_weight="1"
,等於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>
OUTPUT:
#。如果你想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);
}
}
OUTPUT:
希望這將有助於〜
0
試試這個 創建佈局文件這樣
<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>
Java代碼
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);
}
}
}
問我的情況下,OB任何查詢
相關問題
- 1. 在佈局中分隔EditText和TableRows
- 2. 如何實現間隔均勻佈局
- 3. Qt佈局間隔尺寸
- 4. 如何在Android佈局中製作「分隔符」/「尺子」?
- 5. 如何更新部分間隔時間的cshtml共享佈局?
- 6. 佈局無效(EditText)
- 7. 如何訪問放置在爲AlertDialog製作的自定義佈局中的EditText?
- 8. 如何將EditText與佈局對齊?
- 9. 如何製作固定的XML佈局?
- 10. 如何製作均勻間隔的NSSegmentedControl?
- 11. 平等垂直間隔的列布局
- 12. 添加間隔物線佈局的PyQt
- 13. 佈局中間隔均勻的按鈕
- 14. EditText框的正確佈局
- 15. EditText的Android佈局問題?
- 16. 安卓佈局 - 製作EditText行尺寸均勻
- 17. 如何製作線性佈局半框佈局和半格佈局?
- 18. 如何製作透明佈局?
- 19. 如何製作UICollectionView自動佈局
- 20. 如何製作方形框架佈局?
- 21. 如何製作半透明佈局?
- 22. 我該如何製作這種佈局?
- 23. 如何製作彈性垂直佈局?
- 24. UICollectionView佈局,單元間隔很遠
- 25. 間隔項目在QML佈局
- 26. Android佈局 - 製作可滾動佈局
- 27. EditText和按鈕佈局
- 28. Android中的自定義EditText:如何在XML佈局中引用?
- 29. 如何使用我自己的自定義EditText佈局作爲搜索查看
- 30. D3.js強制佈局:如何隔離節點組?