0

I have 4 EditText's now I want to put text in it on the one that is focussed and after that I want to go to the next EditText

anyone an idea how I can do this?

what I want:

focussedEditText.setText("hi");
focusOnNextEditText;
Rikkert09
  • 190
  • 1
  • 1
  • 11
  • an Edittext has focus when its being flirted with _(i kkonw you already know that).. but i am thinking you are having 4 edittext text, which any one of which can gain focus..right? – Elltz Oct 30 '14 at 13:24

4 Answers4

0

This might help

android.view.View.OnFocusChangeListener;
edittext.setOnFocusChangeListener(myEditTextFocus);

and requestFocus() too.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Shriram
  • 4,343
  • 8
  • 37
  • 64
0
edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
    edit_Text.setText("Your text");
    Second_edit_text.requestFocus();

 }else {

}});
Michael A
  • 5,770
  • 16
  • 75
  • 127
0

I think this is what you want..

EditText e1,e2;
Button b1,b2;
String evalue;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    e1=(EditText)findViewById(R.id.editText1);
    e2=(EditText)findViewById(R.id.editText2);
    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);

    e1.setOnTouchListener(new View.OnTouchListener()
    {
        public boolean onTouch(View arg0, MotionEvent arg1)
        {
            evalue="1";
            return false;
        }
    });

    e2.setOnTouchListener(new View.OnTouchListener()
    {
        public boolean onTouch(View arg0, MotionEvent arg1)
        {
            evalue="2";
            return false;
        }
    });
    b1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View arg0) {
            if(evalue=="1")
            {
                e1.setText("yes");
            }
            if(evalue=="2")
            {
                e2.setText("yes");
            }
        }
    });


    b2.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View arg0) {
            if(evalue=="1")
            {
                e1.setText("No");
            }
            if(evalue=="2")
            {
                e2.setText("No");
            }
        }
    });

}

Am I correct... If yes then please refer this answer for more detail..

Community
  • 1
  • 1
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
0

you have 4 edittext.. which implies that they are views in a viewgroup (Viewgroup is the parent view- or a view that has children- could be linear or realative or frame even viewflipper layout)? so to this

Viewgroup.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            Viewgroup.getFocusedChild().setText("Ellt's solution works");
            return false;
        }
    });

correct silly errors

EDIT 1: well all you have to do is override it-(but this will only be called once for a viewgroup), it when it gets focus.. but after that it doesnt loose it..

you can always implement it your way, i dont really know how your code is constructed but you can always get the focused child by Viewgroup.getFocusedChild() and you can also do this

private void customfocuses(View v){   // the view here can be replaced with EditText directly- but for wider range i used View   
   v.setText("Ellt's solution works");
   Viewgroup.focusSearch(v,View.FOCUS_DOWN);
   v.clearFocus(); // remove if you want..just wanted to follow code structure..
   customfocuses(Viewgroup.getFocusedChild());
}

all this does is it sets the text to the view and also, tells the viewgroup to find a next view that wants focus and gives it to it and the whole scenario begins again with customfocuses(Viewgroup.getFocusedChild()); also the View.FOCUS_DOWN specifies the direction of the searching for focus views..

So, then you call this the first time you need it.. customfocuses(Viewgroup.getFocusedChild());

what the clearFocus() does is it removes focus from a view.. thats about it

Note: the Viewgroup in ma code are to be replaced with actually viewgroup not the class as i did..

let me know if i it ws good..okay, Sir? lol..

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • yeah this is what i want but then with an OnFocusChangeListener because you never touch the edittext :p – Rikkert09 Oct 30 '14 at 13:51