我们有 2 个屏幕:第一个屏幕包含一个用于显示下一个屏幕的按钮和一个ListView
. 这些ListView
项目起初是可聚焦的。但是当从第二个屏幕返回到第一个屏幕(调用setContentView( )
)时,似乎列表无法再聚焦了。
我很确定这是一个 Android 4.0 错误,只是检查是否有其他人遇到过。这是代码片段:
public class ListTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
createFirstScreen();
createSecondScreen();
setContentView(mFirstScreen);
}
public void createFirstScreen()
{
mFirstScreen = new LinearLayout(this);
mNextBtn = new Button(this);
mNextBtn.setText("Next");
mNextBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setContentView(mSecondScreen);
}
});
mListView = new ListView(this);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
mListView.setAdapter(adapter);
mFirstScreen.addView(mNextBtn);
mFirstScreen.addView(mListView);
}
public void createSecondScreen()
{
mSecondScreen = new LinearLayout(this);
mBackBtn = new Button(this);
mBackBtn.setText("Back");
mBackBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setContentView(mFirstScreen);
}
});
mSecondScreen.addView(mBackBtn);
}
private LinearLayout mFirstScreen;
private ListView mListView;
private Button mNextBtn;
private Button mBackBtn;
private LinearLayout mSecondScreen;
}