图片按钮不可点击 - Android
Image Button is not clickable - Android
我不明白为什么我的按钮不可点击。我在 gridview 中有一个歌曲列表,当用户单击列表中的一个项目时,应用程序会将它们带到我的 SongDetailFragment activity,其中包含有关歌曲的更多信息。我有一个星形按钮,如果数据库中有歌曲,我希望它像这样打开 btn_star_big_on,如果它不在数据库中,我希望它关闭 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){
}
}
看起来您正在使用 ImageView
,而您可能需要 ImageButton
。或者您可以将 android:clickable="true"
添加到 XML.
你所要做的就是 selector
说 btn_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());
.
.
.
}
});
我不明白为什么我的按钮不可点击。我在 gridview 中有一个歌曲列表,当用户单击列表中的一个项目时,应用程序会将它们带到我的 SongDetailFragment activity,其中包含有关歌曲的更多信息。我有一个星形按钮,如果数据库中有歌曲,我希望它像这样打开 btn_star_big_on,如果它不在数据库中,我希望它关闭 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){
}
}
看起来您正在使用 ImageView
,而您可能需要 ImageButton
。或者您可以将 android:clickable="true"
添加到 XML.
你所要做的就是 selector
说 btn_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());
.
.
.
}
});