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();
}
});
}
你好!我想问一下如何在点击按钮时将焦点集中到EditText上。场景是我有一堆EditTexts和Buttons。每个按钮对应一个特定的EditText。我想要的是,例如,如果我点击button1,EditText1将被聚焦。然后当我单击其他按钮时,EditText对应于该按钮将被聚焦,然后其他EditTexts将被禁用。
答案 0 :(得分:8)
OnFocusChangeListener
的代码必须稍加修改,对于您需要最终引用您想要修改的任何对象的匿名类,因为您要修改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};
现在为OnClickListener
做这样的
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!!!
}
});
}
<强>更新强>
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
}
}
});
答案 1 :(得分:3)
内部
onClick()
{
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
}
答案 2 :(得分:1)
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setEnabled(false);
}
所以,首先你需要改变代码
@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
}
}
每次按EditText
Button
都会被停用