[英]setOnFocusChangeListener not being called in Fragment Android
In my fragment setOnFocusChangeListener() is no being called. 在我的片段中,没有调用setOnFocusChangeListener()。 What could be the reason? 可能是什么原因? onToch and onClick are working fine. onToch和onClick运行正常。 Here's my code: 这是我的代码:
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();
}
}
});
}
The Problem is the switch case. 问题是开关盒。 Remove the switch case and it will work. 拆下开关盒即可。 View's id returned in your FocusChange listener will be of editText_barcode
always.So your switchcase
is failing since you are trying to match with another id. 在FocusChange侦听器中返回的View的ID始终为editText_barcode
由于您尝试与另一个ID匹配,因此switchcase
条件失败。
try this... 尝试这个...
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
}
}
Focus is generally requested whenever that view is loaded on screen and you need to grab attention of user.For a view to get focus need to add two attribute android:focusable
and android:focusableInTouchMode
with that View and then requestFocus 通常只要在屏幕上加载该视图时就需要请求焦点,并且需要引起用户的注意。要获得视图的焦点,需要在该视图中添加两个属性android:focusable
和android:focusableInTouchMode
,然后requestFocus
<EditText android:id="@+id/editText_barcode"
...required attributes
android:focusable="true"
android:focusableInTouchMode="true">
<requestFocus />
</EditText>
Also your are setting onFocusChangeListener()
as below make sure it is View.onFocusChangeListener()
So suggest you to change below code 另外,您正在如下设置onFocusChangeListener()
,以确保它是View.onFocusChangeListener()
因此建议您更改以下代码
editText_barcode.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
to 至
editText_barcode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
Also after checking whether hasFocus
I would request you to change it to 同样在检查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();
}
setOnFocusChangeListener
is buggy sometimes. setOnFocusChangeListener
有时会出现setOnFocusChangeListener
。 Try setOnTouchListener
. 尝试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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.