Android Move cursor from one EditText to another one?

随声附和 提交于 2020-04-29 19:40:19

问题


I know these type of question asked many time .but still nobody gave perfect answer for that.

I have question :

I want to move from EditText1 ** to another **EditText2 . I had already detect to editText1 but how to move cursor to editText2.?

In short I had to move my cursor position from one editText1 to another EditText2 directly.


回答1:


I faced this type of issue and found the solution as below.

Here I have two editText ,

if I press "a", my cursor will move to next step . I used below code for doing it.

 final EditText editText = (EditText) findViewById(R.id.editText1);

       editText.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v , int keyCode , KeyEvent event) {

                  EditText editText2 = (EditText) findViewById(R.id.editText2);

                // TODO Auto-generated method stub
                if (keyCode == event.KEYCODE_A) {

                    Selection.setSelection((Editable) editText2.getText(),editText.getSelectionStart());
                    editText2.requestFocus();
                }

                return true;
            }
        });

let me know if you are facing any error regarding this. If my answer is helpful to you please accept it and rate to upvote.




回答2:


Here is a working example, hope this helps.

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText 
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some string of text"
        />

    <EditText 
        android:id="@android:id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some string of text"
        />

    <Button 
        android:id="@android:id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        />

</LinearLayout>

Class:

public class Example extends Activity {
    TextView text1;
    TextView text2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text1 = (EditText) findViewById(android.R.id.text1);
        text2 = (EditText) findViewById(android.R.id.text2);
        Button button = (Button) findViewById(android.R.id.button1);

        button.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Selection.setSelection((Editable) text2.getText(), text1.getSelectionStart());
                text2.requestFocus();
            }
        });
    }
}



回答3:


Set onKeyListener to detect the key pressed on every key pressed checked your condition and when your condition will be fulfilled set edittext property edittext2.requestFocus();




回答4:


For this, all you need to do is...add below two properties to your EditText tag in xml, except the last EditText(In case, you add it to the last EditText also, then the cursor control will again go to the first EditText when you press enter/next from the keypad)

<EditText . . android:singleLine="true" android:imeOptions="actionNext" . . />

Hope this helps




回答5:


I have tested all the previous code segments, and find all are working fine. But I find just calling "requestFocus()" with the proper edittext object is also working. As per as ques asked, the ans can be:

edittext2.requestFocus();

which is working fine for me. Please correct me if I am wrong.



来源:https://stackoverflow.com/questions/10398692/android-move-cursor-from-one-edittext-to-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!