2017-07-03 46 views

回答

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:

enter image description here

#。如果你想move光標programmaticallyenable/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:

enter image description here

希望這將有助於〜

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任何查詢