我的列表视图的顶部有按钮。在我从列表中选择一项之前,这些按钮不起作用。根据其他一些帖子,我应该将以下行添加到我的XML文件中: android:focusable="false“。但是,添加该行后没有发生任何更改。
下面是我的XML文件:
<LinearLayout 
    android:layout_weight="2" 
    android:layout_height="fill_parent" 
    android:layout_width="match_parent" 
    >
<VideoView 
    android:id="@+id/video_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout> 
<LinearLayout 
    android:layout_weight="1" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:orientation="vertical"  >  
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/chapters"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:focusable="false"
            android:text="@string/chapters" /> 
        <Button
            android:id="@+id/scales"
            android:layout_width="100dp"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:focusable="false"
            android:text="@string/scales" />
        <Button
            android:id="@+id/b_flat"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/b_flat" /> 
        <Button
            android:id="@+id/e_flat"
            android:layout_width="75dp"
            android:layout_height="@dimen/btn_layout_height"    
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/e_flat" /> 
        <Button
            android:id="@+id/concert"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/concert" />
        <Button
            android:id="@+id/bass"
            android:layout_width="75dp"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/bass" />  
        <Button
            android:id="@+id/video"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/video" />
        <Button
            android:id="@+id/practice"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"    
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/practice" />                                                      
    </LinearLayout>
<FrameLayout 
          android:id="@+id/fragmentContainer"
          android:layout_height="match_parent"
          android:layout_width="match_parent" 
          />
</LinearLayout>
下面是我用来显示列表的代码:
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);        
}我将我的"onListItemClick“更新为:
    @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();
}}
发布于 2013-05-19 10:53:19
您的Button是在onListItemClick中实例化的,所以只有在您单击列表项之后,它们才会被clickcable。您可以将它们移动到onCreate(),但随后还需要使用inflate将另一个View放入其中,使用setContentView()
此外,正如buptcoder的评论中所说,我没有看到Button上附加了OnClickListener。在您将它移出监听器之后,我仍然看不到它是可点击的,但它现在在监听器中,但是仍然需要将OnClickListener附加到Button
发布于 2013-05-19 10:54:15
这些按钮对任何东西都不起作用,不仅仅是ListView。
为了让点击(我假设这就是你想要的)工作,让你的Activity实现View.OnClickListener,在onClick方法中实现你的点击处理逻辑(这里有更详细的https://developer.android.com/guide/topics/ui/ui-events.html),在onCreate中,这样做:
Button b = (Button) findViewById(R.id.concert);
b.setOnClickListener(this);https://stackoverflow.com/questions/16630941
复制相似问题