如何在android中创建类似windows输入键的编辑文本

时间:2016-04-06 11:34:16

标签: android xml

我需要一个编辑文本,当我输入2位数字时,它会自动进入下一行,这里是我要创建的图像。可以为此提供帮助或者告诉我如何实现这种情况

enter image description here

3 个答案:

答案 0 :(得分:2)

尝试以下解决方案。

首先创建布局(在我的案例中为activity_main.xml),如下所示。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Enter phone number" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="3"
            android:gravity="center"
            android:inputType="phone"
            android:maxLength="2" />

        <TextView
            android:layout_width="10dp"
            android:layout_height="1dp"
            android:layout_gravity="center_vertical"
            android:background="#000" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="3"
            android:gravity="center"
            android:inputType="phone"
            android:maxLength="2" />

        <TextView
            android:layout_width="10dp"
            android:layout_height="1dp"
            android:layout_gravity="center_vertical"
            android:background="#000" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="3"
            android:gravity="center"
            android:inputType="phone"
            android:maxLength="2" />
    </LinearLayout>
</RelativeLayout>

然后将以下代码添加到您的活动中(在我的案例中为MainActivty.java)。

    import android.app.Activity;
    import android.content.Context;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextUtils;
    import android.text.TextWatcher;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.EditText;

   public class MainActivity extends AppCompatActivity {

    private EditText editText1, editText2, editText3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText3 = (EditText) findViewById(R.id.editText3);

        editText1.addTextChangedListener(new MyTextWatcher(editText1));
        editText2.addTextChangedListener(new MyTextWatcher(editText2));
        editText3.addTextChangedListener(new MyTextWatcher(editText3));

        editText1.setOnKeyListener(new MyKeyListener());
        editText2.setOnKeyListener(new MyKeyListener());
        editText3.setOnKeyListener(new MyKeyListener());
    }

    private class MyKeyListener implements View.OnKeyListener{

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                switch (v.getId()) {
                    case R.id.editText3:
                        if (TextUtils.isEmpty(editText3.getText()))
                            editText2.requestFocus();
                        break;

                    case R.id.editText2:
                        if (TextUtils.isEmpty(editText2.getText()))
                            editText1.requestFocus();
                        break;

                }
            }

            return false;
        }
    }

    private class MyTextWatcher implements TextWatcher {


        private final EditText mEditText;

        public MyTextWatcher(EditText editText){
            this.mEditText = editText;
        }

        @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) {
            switch (mEditText.getId()){
                case R.id.editText1:
                    if (!TextUtils.isEmpty(mEditText.getText()) && mEditText.getText().length() == 2)
                        editText2.requestFocus();
                    break;
                case R.id.editText2:
                    if (!TextUtils.isEmpty(mEditText.getText()) && mEditText.getText().length()  == 2)
                        editText3.requestFocus();
                    break;
                case R.id.editText3:
                    if (!TextUtils.isEmpty(mEditText.getText()) && mEditText.getText().length() == 2)
                        hideKeyboard(MainActivity.this);
                    break;
            }


        }
    };



    public static void hideKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}

答案 1 :(得分:0)

试试这个,

编辑maxlenght编辑文本1

edtPin1.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0) {

                edtPin2.requestFocus();
            }

        }
    });

答案 2 :(得分:0)

maxlenght放入最后editText

editText1.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 (s.toString().length() == 2) {

                    editText2.requestFocus();
                }
            }
        });

editText2.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 (s.toString().length() == 2) {

                    editText3.requestFocus();
                }
            }
        });