Reputation: 43
int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};
for (int editText : etColorId) {
findViewById(editText).setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setEnabled(false);
}
});
}
int[] btnEditId = new int[] {R.id.btnEdit1, R.id.btnEdit2, R.id.btnEdit3, R.id.btnEdit4, R.id.btnEdit5,
R.id.btnEdit6, R.id.btnEdit7, R.id.btnEdit8, R.id.btnEdit9, R.id.btnEdit10, R.id.btnEdit11,
R.id.btnEdit12, R.id.btnEdit13, R.id.btnEdit14, R.id.btnEdit15};
for (int button : btnEditId) {
findViewById(button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setEnabled(true);
v.requestFocus();
}
});
}
How can I gain focus to EditText upon clicking a button? I have a bunch of EditTexts and Buttons. Each button corresponds to a specific EditText. What I want is, for example: if I clicked the button1, the EditText1 will be focused. And then when I click other button, the EditText corresponds to that button will be focused and then the other EditTexts will be disabled.
Upvotes: 3
Views: 16923
Reputation: 14203
This code for OnFocusChangeListener
has to be modified little bit, for anonymous classes you need to have final reference of any object which you wish to modify, since you want to modify EditText
final int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};
Now for OnClickListener
do like this
for (int i=0;i<btnEditId.length;i++)
{
final int counter=i;
int button=btnEditId[i];
findViewById(button).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// HERE v refers to button itself on which you clicked,
// you need to update get edit text so
// based on ith position accessing the same edit as the button correspond too
EditText edt=etColorId[counter];
edt.setEnabled(true);
edt.requestFocus();
// and you are DONE!!!
}
});
}
UPDATE:
new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if(!hasFocus)
{ // lost focus
v.setEnabled(false);
}
else
{
//you are already enabling on button click
}
}
});
Upvotes: 9
Reputation: 2215
onFocusChange called every time when lost focus and gain focus.
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setEnabled(false);
}
So, first you need to change code
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus) {// gain focus
// Code block
}
if(!hasFocus) { // lost focus
// Code block
}
}
You EditText
is disabled every time, when press Button
Upvotes: 1
Reputation: 11194
Inside
onClick()
{
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
}
Upvotes: 4