Java Android Edittext:setOnFocusChangeListener 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20215478/
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-08-13 00:22:11  来源:igfitidea点击:

Android Edittext : setOnFocusChangeListener doesn't work

javaandroidandroid-edittext

提问by Aoyama Nanami

i'm using this code for checking my edittext focus or not :

我正在使用此代码检查我的编辑文本焦点与否:

 gelar_pp=(EditText)polis.findViewById(R.id.gelar_pp);
    gelar_pp.setOnFocusChangeListener(new OnFocusChangeListener() {
    LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
            }else {
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
            }
        } });
    ibu_pp=(EditText)polis.findViewById(R.id.ibu_pp);
    ibu_pp.setOnFocusChangeListener(new OnFocusChangeListener() {
    LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
            }else {
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
            }
        } });
    edit_bukti_lain_pp=(EditText)polis.findViewById(R.id.edit_bukti_lain_pp);
    edit_bukti_lain_pp.setOnFocusChangeListener(new OnFocusChangeListener() {
    LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
            }else {
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
            }
        } });

when i make simple my code, and change into :

当我简化我的代码并更改为:

    gelar_pp.setOnFocusChangeListener(listener);
ibu_pp.setOnFocusChangeListener(listener);
edit_bukti_lain_pp.setOnFocusChangeListener(listener);
        listener= new OnFocusChangeListener() {    
            LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
                }else {
                    layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
                }
            }
        };

my code is not working, nothing change. is there any wrong with my code?

我的代码不起作用,没有任何变化。我的代码有什么问题吗?

采纳答案by Gopal Gopi

First initialize the listener and then set to EditTexts like

首先初始化侦听器,然后设置为 EditTexts 之类的

listener= new OnFocusChangeListener() {    
        LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
            }else {
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
            }
        }
    };
gelar_pp.setOnFocusChangeListener(listener);
ibu_pp.setOnFocusChangeListener(listener);
edit_bukti_lain_pp.setOnFocusChangeListener(listener);

and set focusable property to truefor EditTexts if not set previously like...

并将可聚焦属性设置true为 EditTexts 如果之前未设置,例如...

editText.setFocusable(true);

回答by Tombeau

Try like this

像这样尝试

edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus){
        Toast.makeText(getApplicationContext(), "on focus", Toast.LENGTH_LONG).show();
    }else {
        Toast.makeText(getApplicationContext(), "lost focus", Toast.LENGTH_LONG).show();
    }
   }
});

回答by Dayanand Waghmare

Add in XML

在 XML 中添加

android:focusable="true"
android:focusableInTouchMode="true"