Android 按钮不使用listview
我的listview顶部有按钮。在我从列表中选择一个项目之前,按钮不起作用。根据其他一些帖子,我应该在XML文件中添加以下行:android:focusable=“false”。但是,添加该行后没有发生任何更改 这是我的XML文件:Android 按钮不使用listview,android,button,android-listview,android-fragments,Android,Button,Android Listview,Android Fragments,我的listview顶部有按钮。在我从列表中选择一个项目之前,按钮不起作用。根据其他一些帖子,我应该在XML文件中添加以下行:android:focusable=“false”。但是,添加该行后没有发生任何更改 这是我的XML文件: }您的按钮在您的onListItemClick中实例化,因此在单击列表项之前,它们不会是clickcable。您可以将它们移动到onCreate(),但随后还需要使用setContentView() 另外,正如buptcoder在评论中所说,我没有看到按钮上附加了
}您的
按钮
在您的onListItemClick
中实例化,因此在单击列表项之前,它们不会是clickcable
。您可以将它们移动到onCreate()
,但随后还需要使用setContentView()
另外,正如buptcoder在评论中所说,我没有看到
按钮
上附加了OnClickListener
。我仍然看不到它是可点击的,直到你把它移出监听器,它现在在监听器中,但是仍然需要将一个OnClickListener
附加到按钮上这些按钮不能使用任何东西,而不仅仅是一个列表视图
要使单击(我假设这就是您想要的)正常工作,请让您的活动实现View.OnClickListener,并在onClick方法(此处更详细)中实现单击处理逻辑,然后在onCreate中执行以下操作:
Button b = (Button) findViewById(R.id.concert);
b.setOnClickListener(this);
谢谢你的帮助。我将onClick的代码移动到最初调用的活动中。我有两个活动,所以我必须确定正确的一个。现在按钮有了焦点,onClick可以工作了。我有onClickListener代码,但显然没有在我的帖子中包含它。(我的错误)在将按钮移出onListItemClick并移入“我的onCreate”活动后,这些按钮确实具有焦点。谢谢你的帮助。
public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";
private Button mChaptersButton;
@Override
public void onCreate(Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.chapters_title);
mChapters = ChapterList.get(getActivity()).getChapters();
ChapterAdapter adapter = new ChapterAdapter(mChapters);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//if (VERBOSE) Log.v(TAG, "+++ onListItemClick +++");
// Get the chapter from the adapter
Chapters c = ((ChapterAdapter)getListAdapter()).getItem(position);
//Start ImprovisationActivity
Intent i = new Intent(getActivity(), ImprovisationActivity.class);
i.putExtra(ChapterFragment.EXTRA_CHAPTER_ID, c.getId());
startActivity(i);
}
private class ChapterAdapter extends ArrayAdapter<Chapters> {
public ChapterAdapter(ArrayList<Chapters> chapters) {
super(getActivity(), 0, chapters);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.list_item_chapter, null);
}
// Configure the view for this Chapter
Chapters c = getItem(position);
TextView titleTextView =
(TextView) convertView.findViewById(R.id.chapter_list_item_titleTextView);
titleTextView.setText(c.getChapter());
return convertView;
}
}
@Override
public void onResume() {
super.onResume(); {
((ChapterAdapter)getListAdapter()).notifyDataSetChanged();
}
}
public class ChapterListActivity extends SingleFragmentActivity {
private static final boolean VERBOSE = true;
private static final String TAG = "ChapterListActivity";
private Button mChaptersButton;
@Override
public void onCreate(Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_improvisation);
mChaptersButton = (Button)findViewById(R.id.chapters);
// Need to default the button to pressed
mChaptersButton.setSelected(true);
mChaptersButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mChaptersButton.isSelected()){
if (VERBOSE) Log.v(TAG, "+++ onClick for mChaptersButton +++");
mChaptersButton.setSelected(false);
Toast.makeText(getApplicationContext(), "Chapters Button Not Selected",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Chapters Button Selected",
Toast.LENGTH_SHORT).show();
mChaptersButton.setSelected(true);
}
}
});
}
@Override
protected Fragment createFragment() {
if (VERBOSE) Log.v(TAG, "+++ createFragment +++");
return new ChapterListFragment();
}
Button b = (Button) findViewById(R.id.concert);
b.setOnClickListener(this);