Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select next and previous Edit texts in a view

I have five Edit Text in my application . I also have two buttons called "Next" and "Previous". Now I want to select the next and previous edit text fields when i click the corresponding buttons form my view dynamically. Is there any way to do this.

like image 630
Sniper Avatar asked Mar 19 '12 11:03

Sniper


3 Answers

btnNext.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        int id = getCurrentFocus().getNextFocusDownId();
        if(id != View.NO_ID) {
            findViewById(id).requestFocus();
            System.out.println("Next");
        }
    }
});

btnBack.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        int id = getCurrentFocus().getNextFocusUpId();
        if(id != View.NO_ID) {
            findViewById(id).requestFocus();
            System.out.println("Back");
        }
    }
});

This is the XML where you have to set the focus order

<EditText
    android:id="@+id/et1"
    android:nextFocusDown="@+id/et2"
    android:nextFocusUp="@+id/et2"
    ....../>

<EditText
    android:id="@+id/et2"
    android:nextFocusDown="@+id/et1"
    android:nextFocusUp="@+id/et1"
    ...../>

Edit

If you are creating view dynamic then you should use below methods to set the next focus

setNextFocusDownId(id)
setNextFocusUpId(id);
like image 74
Dharmendra Avatar answered Oct 14 '22 10:10

Dharmendra


i think this may help you,

http://kahdev.wordpress.com/2008/06/29/changing-button-text-in-android/

like image 38
Hasmukh Avatar answered Oct 14 '22 11:10

Hasmukh


Try -

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_next :
            if(editText1.hasFocus()){
                editText2.requestFocus();
            }else if(editText2.hasFocus()){
                editText3.requestFocus();
            }
            break;
        case R.id.btn_previous :
            if(editText2.hasFocus()){
                editText1.requestFocus();
            }else if(editText3.hasFocus()){
                editText2.requestFocus();
            }
            break;
    }
}
like image 3
Rajkiran Avatar answered Oct 14 '22 10:10

Rajkiran