首页 > 解决方案 > 有没有办法在android studio中使用多个edittext和自定义数字键盘

问题描述

我一直在研究具有多个edittext的android项目,这些edittext从我创建的自定义数字按钮中获取其值。

我希望当我单击数字时,它应该按时间顺序显示编辑文本中的数字。我已经尝试了很多,但没有奏效。这是我的图像目标
最后的样子
有人可以帮助我吗?

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".chooseNumberActivity">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/choosetoolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        app:titleTextColor="@color/colorPrimary"

        app:titleMarginStart="30dp"
        android:background="@drawable/edittext_background"
        />


   <GridLayout
       android:id="@+id/inputGrids"
       android:layout_below="@+id/choosetoolbar"
       android:layout_width="match_parent"
       android:padding="10dp"
       android:layout_height="80dp">
      <EditText
          android:id="@+id/firstValue"

          android:background="@drawable/pinbackground"
          android:layout_width="50dp"
          android:layout_height="wrap_content"
         android:hint="00"
          android:maxLength="2"
          android:textAlignment="center"
          android:imeOptions="actionNext"
          android:cursorVisible="false"
          android:enabled="false"
          />
      <EditText
          android:id="@+id/secondValue"
          android:hint="00"
          android:layout_marginStart="7dp"
          android:background="@drawable/pinbackground"
          android:layout_width="50dp"
          android:layout_height="wrap_content"
          android:textAlignment="center"

          android:maxLength="2"
          android:imeOptions="actionNext"
          android:cursorVisible="false"
          android:enabled="false"
          />
      <EditText
          android:id="@+id/thirdValue"
          android:layout_marginStart="7dp"
          android:background="@drawable/pinbackground"
          android:layout_width="50dp"
          android:textAlignment="center"
          android:layout_height="wrap_content"
        android:hint="00"
          android:maxLength="2"
          android:imeOptions="actionNext"
          android:cursorVisible="false"
          android:enabled="false"
          />
      <EditText
          android:id="@+id/fourthValue"
          android:layout_marginStart="7dp"
          android:background="@drawable/pinbackground"
          android:layout_width="50dp"
          android:layout_height="wrap_content"
       android:hint="00"
          android:maxLength="2"
          android:imeOptions="actionNext"
          android:cursorVisible="false"
          android:enabled="false"
          />
      <EditText
          android:id="@+id/fivthValue"
          android:layout_marginStart="7dp"
          android:textAlignment="center"
          android:background="@drawable/pinbackground"
          android:layout_width="50dp"
          android:layout_height="wrap_content"
         android:hint="00"
          android:maxLength="2"
          android:imeOptions="actionNext"
          android:cursorVisible="false"
          android:enabled="false"
          />
      <EditText
          android:id="@+id/sixthValue"
          android:layout_marginStart="7dp"
          android:background="@drawable/pinbackground"
          android:layout_width="50dp"
          android:textAlignment="center"
          android:layout_height="wrap_content"
       android:hint="00"
          android:maxLength="2"
          android:imeOptions="actionNext"
          android:cursorVisible="false"
          android:enabled="false"
          />

      <EditText
          android:id="@+id/seventhValue"
          android:layout_marginStart="7dp"
          android:textAlignment="center"
          android:background="@drawable/pinbackground"
          android:layout_width="50dp"
          android:layout_height="wrap_content"
          android:cursorVisible="false"
          android:enabled="false"
          android:layout_marginEnd="5dp"
          android:imeOptions="actionNext"
        android:hint="00"
          android:maxLength="2" />



   </GridLayout>




        <GridLayout
            android:id="@+id/gridlayout"
           android:layout_below="@+id/inputGrids"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:useDefaultMargins="false"
            android:alignmentMode="alignBounds"
            android:columnCount="7"
            android:rowCount="7"
            android:padding="10dp"
            android:layout_marginBottom="120dp"
            android:layout_marginStart="2dp"
            android:layout_marginEnd="2dp"

            android:outlineAmbientShadowColor="@color/colorPrimary"
            >
         <Button
            android:id="@+id/btn1"
             android:layout_width="30dp"
             android:layout_height="30dp"
           android:background="@drawable/n1"
             android:textStyle="bold"
             android:layout_row="0"
             android:layout_column="0"
             android:layout_rowWeight="1"
             android:layout_columnWeight="1"
             />
            <Button
                android:id="@+id/btn2"
             android:background="@drawable/n2"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="0"
                android:layout_column="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn3"
                android:background="@drawable/n3"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="0"
                android:layout_column="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn4"
                android:background="@drawable/n4"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="0"
                android:layout_column="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn5"
                android:background="@drawable/n5"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="0"
                android:layout_column="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn6"
                android:background="@drawable/n6"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="0"
                android:layout_column="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn7"
               android:background="@drawable/n7"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="0"
                android:layout_column="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />

            <Button
                android:id="@+id/btn8"
                android:background="@drawable/n8"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="1"
                android:layout_column="0"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn9"
                android:background="@drawable/n9"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="1"
                android:layout_column="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn10"
                android:background="@drawable/n10"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="1"
                android:layout_column="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn11"
                android:background="@drawable/n11"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="1"
                android:layout_column="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn12"
                android:background="@drawable/n12"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="1"
                android:layout_column="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn13"
                android:background="@drawable/n13"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="1"
                android:layout_column="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn14"
                android:background="@drawable/n14"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="1"
                android:layout_column="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn15"
                android:background="@drawable/n15"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="2"
                android:layout_column="0"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn16"
                android:background="@drawable/n16"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="2"
                android:layout_column="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn17"
                android:background="@drawable/n17"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="2"
                android:layout_column="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn18"
                android:background="@drawable/n18"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="2"
                android:layout_column="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn19"
                android:background="@drawable/n19"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="2"
                android:layout_column="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn20"
                android:background="@drawable/n20"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="2"
                android:layout_column="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn21"
                android:background="@drawable/n21"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="2"
                android:layout_column="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn22"
                android:background="@drawable/n22"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="3"
                android:layout_column="0"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn23"
                android:background="@drawable/n23"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="3"
                android:layout_column="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn24"
                android:background="@drawable/n24"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="3"
                android:layout_column="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn25"
                android:background="@drawable/n25"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="3"
                android:layout_column="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn26"
                android:background="@drawable/n26"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="3"
                android:layout_column="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn27"
                android:background="@drawable/n27"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="3"
                android:layout_column="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
            <Button
                android:id="@+id/btn28"
                android:background="@drawable/n28"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_row="3"
                android:layout_column="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                />
          
        </GridLayout>

   <Button
       android:id="@+id/delete"
       android:layout_width="68dp"
       android:layout_height="wrap_content"
       android:layout_alignParentEnd="true"
       android:layout_alignParentBottom="true"
       android:layout_marginTop="24dp"
       android:layout_marginEnd="160dp"
       android:layout_marginBottom="52dp"
       android:paddingBottom="10dp" />

   <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentStart="true"
       android:layout_alignParentEnd="true"
       android:layout_alignParentBottom="true"
       android:layout_marginStart="35dp"
       android:layout_marginEnd="288dp"
       android:layout_marginBottom="54dp"
       android:text="Button" />


</RelativeLayout>

这是我的java代码

public class chooseNumberActivity extends AppCompatActivity {
    private Button n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18,n19,n20,n21,n22,n23,n24,n25,n26,n27,n28,n29,n30,n31,
    n32,n33,n34,n35,n36,n37,n38,n39,n40,n41,n42,n43,n44,n45,n46,n47,n48,n49;
   private Pinview pin;
   private GridLayout inputgrids;
   private EditText firstValue;
    private EditText secondValue;
    private EditText thirdValue;
    private EditText fourthValue;
    private EditText fifthValue;
    private EditText sixthValue;
    private EditText seventhValue;
   private Toolbar toolbar;
    final int edittextvalues[]=new int[6];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_number);
        Initialize();
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Choose Your Number");

        firstValue.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
             if (firstValue==null){
                 firstValue.requestFocus(1);
             }
            }
        });
        secondValue.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (firstValue!=null){
                    secondValue.requestFocus(2);
                }

            }
        });
        thirdValue.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
              if (firstValue!=null&&secondValue!=null){
                  thirdValue.requestFocus(3);
              }
            }
        });
        fourthValue.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
             if (firstValue!=null&&secondValue!=null&&thirdValue!=null){
                 fourthValue.requestFocus(4);
             }
            }
        });
        fifthValue.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (firstValue!=null&&secondValue!=null&&thirdValue!=null&&fourthValue!=null){
                    fifthValue.requestFocus(5);
                }
            }
        });
        sixthValue.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (firstValue!=null&&secondValue!=null&&thirdValue!=null&&fourthValue!=null&&fifthValue!=null){
                   sixthValue.requestFocus(6);
                }
            }
        });
        seventhValue.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (firstValue!=null&&secondValue!=null&&thirdValue!=null&&fourthValue!=null&&fifthValue!=null&&sixthValue!=null){
                    seventhValue.requestFocus(7);
                }
            }
        });


        n1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

         if (firstValue.isFocused()){
             firstValue.setText("1");
         }
                if (secondValue.isFocused()){
                    secondValue.setText("1");
                }
                if (thirdValue.isFocused()){
                    thirdValue.setText("1");
                }
                if (fourthValue.isFocused()){
                    fourthValue.setText("1");
                }
                if (fifthValue.isFocused()){
                    fifthValue.setText("1");
                }
                if (sixthValue.isFocused()){
                   sixthValue.setText("1");
                }
                if (seventhValue.isFocused()){
                    seventhValue.setText("1");
                }


            }
        });
        n2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (firstValue.isFocused()){
                    firstValue.setText("2");
                }
                if (secondValue.isFocused()){
                    secondValue.setText("2");
                }
                if (thirdValue.isFocused()){
                    thirdValue.setText("2");
                }
                if (fourthValue.isFocused()){
                    fourthValue.setText("2");
                }
                if (fifthValue.isFocused()){
                    fifthValue.setText("2");
                }
                if (sixthValue.isFocused()){
                    sixthValue.setText("2");
                }
                if (seventhValue.isFocused()){
                    seventhValue.setText("2");
                }

            }
        });
        n3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (firstValue.isFocused()){
                    firstValue.setText("3");
                }
                if (secondValue.isFocused()){
                    secondValue.setText("3");
                }
                if (thirdValue.isFocused()){
                    thirdValue.setText("3");
                }
                if (fourthValue.isFocused()){
                    fourthValue.setText("3");
                }
                if (fifthValue.isFocused()){
                    fifthValue.setText("3");
                }
                if (sixthValue.isFocused()){
                    sixthValue.setText("3");
                }
                if (seventhValue.isFocused()){
                    seventhValue.setText("3");
                }

            }
        });

标签: javaandroid

解决方案


推荐阅读