java 单击按钮时将焦点设置到 EditText - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25131827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:25:05  来源:igfitidea点击:

Set focus to the EditText upon clicking a button - Android

javaandroiduser-interface

提问by Carlo Dacuyan

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();
            }
        });
    }

Hi guys! I would like to ask If how can I gain focus to EditText upon clicking a button. The scenario is 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.

嗨,大家好!我想问一下,如果单击按钮时如何才能获得对 EditText 的关注。场景是我有一堆 EditTexts 和 Buttons。每个按钮对应一个特定的 EditText。例如,我想要的是,如果我单击 button1,则 EditText1 将被聚焦。然后当我单击其他按钮时,与该按钮对应的 EditText 将被聚焦,然后其他 EditText 将被禁用。

回答by Akhil Jain

This code for OnFocusChangeListenerhas 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

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};

Now for OnClickListenerdo like this

现在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!!!
        }
    });
}

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
                }
            }
  });

回答by KOTIOS

Inside

里面

onClick()
{
    edittext.setFocusableInTouchMode(true);
    edittext.requestFocus();
}

回答by Palak

onFocusChange called every time when lost focus and gain focus.

每次失去焦点和获得焦点时都会调用 onFocusChange。

@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 EditTextis disabled every time, when press Button

EditText每次都被禁用,当按下Button