图片按钮无法点击 - Android

时间:2016-01-06 15:00:26

标签: android button android-imagebutton

我无法弄清楚为什么我的按钮无法点击。我在gridview中有一个歌曲列表,当用户点击列表中的一个项目时,应用程序会将它们带到我的SongDetailFragment活动中,该活动包含有关该歌曲的更多信息。我有一个星形按钮,如果数据库中的一首歌我想要它就像这个btn_star_big_on一样,如果它在数据库中的NOT我想让它关闭btn_star_big_off。问题是,当我运行我的应用程序时,我无法点击按钮。它只是一个静态图像,因为用户应该可以打开和关闭它。

任何人都可以建议如何解决这个问题?

Here是我获得星形按钮的地方:

这是星形按钮的xml:

<ImageView
   android:id="@+id/imgFavBtn"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:clickable="true"
   android:contentDescription="@string/favourite"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:src="@android:drawable/btn_star_big_off" />

这是我的细节片段:

public  class SongDetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
    private  Song song;
    private static final int CURSOR_LOADER_ID = 0;
    ImageView imgViewFavButton;
    Boolean mIsFavourite;

    public SongDetailFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.song_fragment_detail, container, false);


        Intent intent = getActivity().getIntent();
        if (intent != null && intent.hasExtra("song")) {


            song = intent.getParcelableExtra("song");

            ((TextView) rootView.findViewById(R.id.detail_title_textview))
                    .setText(song.getTitle());

            ((TextView)rootView.findViewById(R.id.detail_overview_textview))
                    .setText(song.getDescription());


            ((TextView)rootView.findViewById(R.id.song_date_textview))
                    .setText(song.getdate());




            ImageView imageView = (ImageView) rootView.findViewById(R.id.song_detail_poster_imageview);
            Picasso.with(getActivity()).load(song.getPoster()).into(imageView);
        }

        imgViewFavButton = (ImageView) rootView.findViewById(R.id.imgFavBtn);
        checkFavourites(song);
        imgViewFavButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                Cursor c =
                        getActivity().getContentResolver().query(songContract.songEntry.CONTENT_URI,
                                new String[]{songContract.songEntry._ID},
                                null,
                                null,
                                null);

                    insertData(song);

                 getLoaderManager().initLoader(CURSOR_LOADER_ID, null, songDetailFragment.this);

            }
        });

        return rootView;
    }


    public void insertData(song song){
        ContentValues songValues = new ContentValues();
        songValues.put(songContract.songEntry.COLUMN_ID, song.getsong_id());
        songValues.put(songContract.songEntry.COLUMN_IMAGE, song.getPoster());
        songValues.put(songContract.songEntry.COLUMN_TITLE, song.getTitle());
        songValues.put(songContract.songEntry.COLUMN_OVERVIEW, song.getDescription());
        songValues.put(songContract.songEntry.COLUMN_date, song.getdate());



        getActivity().getContentResolver().insert(songContract.songEntry.CONTENT_URI,
                songValues);

    }


    private void checkFavourites(song song) {
        Cursor c =
                getActivity().getContentResolver().query(songContract.songEntry.CONTENT_URI,
                        null,
                        songContract.songEntry.COLUMN_ID + " = ?",
                        new String[]{song.getsong_id()},
                        null);

        if (c != null) {
            c.moveToFirst();
            int index = c.getColumnIndex(songContract.songEntry.COLUMN_ID);

            if (c.getCount() > 0 && c.getString(index).equals(song.getsong_id())) {
                mIsFavourite = true;
                imgViewFavButton.setImageResource(android.R.drawable.btn_star_big_on);
            }

            else{
                 imgViewFavButton.setImageResource(android.R.drawable.btn_star_big_off);
            }
                c.close();

            }



    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args){
        return new CursorLoader(getActivity(),
                songContract.songEntry.CONTENT_URI,
                null,
                null,
                null,
                null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
    }


    // Set the cursor in our CursorAdapter once the Cursor is loaded
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {




    }

    // reset CursorAdapter on Loader Reset
    @Override
    public void onLoaderReset(Loader<Cursor> loader){

    }

}

2 个答案:

答案 0 :(得分:1)

当您可能需要ImageView时,您似乎正在使用ImageButton。或者您可以将android:clickable="true"添加到XML。

答案 1 :(得分:1)

您要实施的只是selectorbtn_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_star_big_on" android:state_selected="true"/>
    <item android:drawable="@drawable/btn_star_big_off" android:state_selected="false"/>
 </selector>

并将btn_selector设置为ImageView按钮的背景..

并将单行添加到onClickListener ..

imgViewFavButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        imgViewFavButton.setSelected(!imgViewFavButton.isSelected());
        .
        .
        .
    }
});