在Fragment Android中没有调用setOnFocusChangeListener

时间:2015-10-14 05:24:27

标签: android android-fragments

在我的片段中,没有调用setOnFocusChangeListener()。可能是什么原因? onToch和onClick工作正常。 这是我的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_barcode_detail,
            container, false);
editText_barcode = (EditText) rootView
            .findViewById(R.id.editText_barcode);

editText_barcode.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                            editos = (EditText) v;
                            System.out.println("====onf== "+editos);
                            key.showCustomNumKeyboard(v);
                        } else {
                            key.hideCustomNumKeyboard();
                        }
            }

        });
}

4 个答案:

答案 0 :(得分:0)

问题是开关案例。取下开关盒,它会工作。   在FocusChange监听器中返回的视图ID将是   始终editText_barcode。因为您尝试与其他ID匹配,所以switchcase失败了。

答案 1 :(得分:0)

试试这个......

 your activity implement OnFocusChangeListener

 In onCreateView()
 editText_barcode.setOnFocusChangeListener(this);

 @Override
 public void onFocusChange(View v, boolean hasFocus) {
 switch(v.getId()){
 case r.id.editText_barcode:

      //code here

 break;

...etc
    }
}

答案 2 :(得分:0)

每当在屏幕上加载该视图并且您需要引起用户注意时,通常会请求焦点。要获得焦点的视图需要在该视图中添加两个属性android:focusableandroid:focusableInTouchMode,然后requestFocus的

 <EditText android:id="@+id/editText_barcode" 
  ...required attributes
  android:focusable="true"
  android:focusableInTouchMode="true"> 
<requestFocus /> 
</EditText>

您还要设置onFocusChangeListener(),如下所示,请确保View.onFocusChangeListener()建议您更改以下代码

editText_barcode.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override

editText_barcode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override

检查hasFocus后是否会要求您将其更改为

        Log.d(TAG,"EditTextBarcode has Focus value " + hasFocus)
        if (hasFocus) {
                   Log.d(TAG,"EditTextBarcode has Focus")
                   key.showCustomNumKeyboard(v);
                } else {
                   Log.d(TAG,"EditTextBarcode does not have Focus")
                   key.hideCustomNumKeyboard();
                }

答案 3 :(得分:0)

setOnFocusChangeListener有时是越野车。尝试setOnTouchListener

editText_barcode = (EditText) rootView
            .findViewById(R.id.editText_barcode);
editText_barcode.clearFocus()
editText_barcode.setOnTouchListener(new OnTouchListener()() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Your code here
            return false;
        }


});