当 ListView 中的 EditText 时,我发现 EditText 行为的 FocusChange 不同。我的测试程序有两个 EditText。一次在活动上,另一个在列表视图中。
以下是我在普通 Editext 中键入文本并失去焦点时的日志
10-11 16:26:32.440: INFO/Test(4317): 1:onFocusChange:false
10-11 16:26:32.490: INFO/Test(4317): 2:onFocusChange:true
以下是我在列表视图中在 Editext 中键入文本并失去焦点时的日志
10-11 16:28:17.551: INFO/Test(4317): 2:onFocusChange:false
10-11 16:28:17.609: INFO/Test(4317): 2:onFocusChange:true
10-11 16:28:17.679: INFO/Test(4317): 2:onFocusChange:false
10-11 16:28:17.719: INFO/Test(4317): 1:onFocusChange:true
日志告诉我,列表视图中的 EditText 将触发超过 1 次焦点更改。这种行为是否是错误
我使用 android 4.0.3 sdk。
以下是XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText1" android:layout_gravity="center"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"/>
</LinearLayout>
这是代码
public class EditTextActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.edit_text_activity);
final EditText editText = (EditText) findViewById(R.id.editText1);
editText.setWidth(200);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
Log.i("Test", "1:onFocusChange:" + hasFocus);
editText.setText("");
}
});
ListView listView = (ListView) this.findViewById(R.id.listView);
listView.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return 1;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LinearLayout layout = new LinearLayout(viewGroup.getContext());
final EditText editText = new EditText(viewGroup.getContext());
editText.setId(1);
editText.setLayoutParams(new ViewGroup.LayoutParams(200,ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
Log.i("Test", "2:onFocusChange:" + hasFocus);
editText.setText("");
}
});
layout.addView(editText);
return layout;
}
});
}
}