请求另一个EditText的焦点以异常方式运行

时间:2016-06-02 05:40:57

标签: java android

我有四个EditText来读取PIN码。现在我编写了EditText的Java端,它的行为方式异常。在第一个EditText中输入一位数后,它应该请求第二个EditText的焦点,但是它会转到第三个EditText

enter image description here

更进一步将其定义为numberPassword,但数字仍然可见。

以下是布局文件的源代码:

 <RelativeLayout
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<EditText
    android:id="@+id/mpin_1"
    android:layout_marginStart="20dp"
    android:layout_centerVertical="true"
    android:maxLength="1"
    android:gravity="center"
    android:layout_width="50dp"
    android:inputType="numberPassword"
    android:layout_height="50dp" />

<EditText
    android:id="@+id/mpin_2"
    android:layout_marginStart="20dp"
    android:layout_toEndOf="@+id/mpin_1"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:maxLength="1"
    android:layout_width="50dp"
    android:inputType="numberPassword"
    android:layout_height="50dp" />

<EditText
    android:id="@+id/mpin_3"
    android:layout_marginStart="20dp"
    android:layout_toEndOf="@+id/mpin_2"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:layout_width="50dp"
    android:maxLength="1"
    android:inputType="numberPassword"
    android:layout_height="50dp" />

<EditText
    android:id="@+id/mpin_4"
    android:layout_marginStart="20dp"
    android:layout_toEndOf="@+id/mpin_3"
    android:gravity="center"
    android:layout_centerVertical="true"
    android:layout_width="50dp"
    android:maxLength="1"
    android:inputType="numberPassword"
    android:layout_height="50dp" />

</RelativeLayout>

java文件的源代码:

    public class SpinActivity extends AppCompatActivity
{
    private StringBuilder s_pin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spin);
        s_pin = new StringBuilder();
        s_pin.setLength(0);

        Button sPinButton = (Button)findViewById(R.id.set_s_pin_button);

        final EditText e1 = (EditText)findViewById(R.id.mpin_1);
        final EditText e2 = (EditText)findViewById(R.id.mpin_2);
        final EditText e3 = (EditText)findViewById(R.id.mpin_3);
        final EditText e4 = (EditText)findViewById(R.id.mpin_4);

        e1.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(e1.getText().length()==1)
                    s_pin.append(e1.getText().toString());
                //e1.setEnabled(false);
                e2.requestFocus();
                return false;
            }
        });

        e2.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(e2.getText().length()==1)
                    s_pin.append(e2.getText().toString());
                //e2.setEnabled(false);
                e3.requestFocus();
                return false;
            }
        });

        e3.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(e3.getText().length()==1)
                    s_pin.append(e3.getText().toString());
                //e3.setEnabled(false);
                e4.requestFocus();
                return false;
            }
        });

        e4.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(e4.getText().length()==1)
                    s_pin.append(e4.getText().toString());
               // e4.setEnabled(false);
                return false;
            }
        });

        sPinButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getApplicationContext(),s_pin.toString(),
                        Toast.LENGTH_SHORT).show();

            }
        });



    }


}

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

使用以下代码:

android:nextFocusDown="@+id/edittext"

edittext是要关注的EditText的ID。