我在更改将列表项标记为书签的ImageView的状态时遇到了问题。
有人提到要创建一个新的OnClickListener(),但当我单击ImageView时,它确实会进入聚焦状态,但当您完成单击时,它不会更改为按下状态图像。此外,当我单击列表项时,我注意到ImageView再次被聚焦。我不确定我错过了什么:
public class DxSimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
Activity activity;
ImageView image;
public DxSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
long id = getItemId(position);
final ImageView image = (ImageView) view.findViewById(R.id.fav);
image.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
image.setBackgroundResource(R.drawable.ic_fav);
final Button btn = (Button) v.findViewById(R.id.fav);
btn.setPressed(true);
}
});
return view;
}
}我已经为ImageView状态创建了xml,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_star_off_normal"
android:state_checked="false"
android:state_window_focused="false" />
<item android:drawable="@drawable/btn_star_on_normal"
android:state_checked="true"
android:state_window_focused="false" />
<item android:drawable="@drawable/btn_star_on_pressed"
android:state_checked="true"
android:state_pressed="true" />
<item android:drawable="@drawable/btn_star_off_pressed"
android:state_checked="false"
android:state_pressed="true" />
<item android:drawable="@drawable/btn_star_on_focused"
android:state_checked="true"
android:state_focused="true" />
<item android:drawable="@drawable/btn_star_off_focused"
android:state_checked="false"
android:state_focused="true" />
<item android:state_checked="false" android:drawable="@drawable/btn_star_off_normal" />
<item android:state_checked="true" android:drawable="@drawable/btn_star_on_normal" />
</selector>在我的活动中,我创建了以下OnListItemClick()来创建一个弹出的toast消息,但似乎也将焦点放在了ImageView上。
public void onListItemClick(ListView parent, View view, int position, long id) {
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor c = (Cursor) getListAdapter().getItem(position);
String arg = c.getString(c.getColumnIndex("_id"));
Cursor cursor = db.rawQuery("Select category, subcategory From DiagLookup Where _id = ?", new String[]{""+arg});
cursor.moveToFirst();
Context context = getApplicationContext();
CharSequence text = "Category: " + cursor.getString(cursor.getColumnIndex("category")) + "\nSubcategory: " + cursor.getString(cursor.getColumnIndex("subcategory"));
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}澄清我想要发生的事情:
我有两件事要做的列表项目;点击项目,得到一个吐司消息,然后点击一个图像视图,在图像和后端中将图像视图切换到书签状态。
目前,我已经让toast消息工作了,但imageview并没有完全工作...
我已经附加了一个列表项目的屏幕截图,以显示它是如何设置的,并解释当我单击各个区域时发生了什么。

顶部列表项已被点击并保持,我得到的是图像变为聚焦但未点击的图像,如上面的可绘制xml所示,但当我完成点击时,星形变得比其他条目更深的灰色,但它不会变成被点击的状态图像,这是一个黄色的星形。
当我点击其他任何地方,而不是在星星上时,就会弹出吐司消息,这正是我想要发生的,但我也注意到星星也变得聚焦了。我觉得我有一些继承问题,也不能让州政府坚持使用ImageView。
发布于 2012-03-19 08:42:10
因此,您的意思是,您希望在单击图像时出现Toast消息,这就是您希望发生的所有事情,对吧?
如果是这样,这里有一种我会这样做的方法。
在XML中,首先需要确保已经在按钮或图像上设置了onClick。您需要将此代码添加到每个变量中
android:onClick = "Image1"然后在您的java文件中,您需要引用它。您可能希望将OnClickListener实现到您的活动中
public class DxSimpleCursorAdapter extends SimpleCursorAdapter implements OnClickListener{然后在java文件中引用它。
public void Image1 (View v) {
Toast.makeText(this, R.string.TEXT, Toast.LENGTH_SHORT).show();
}您可以使用自己的方法创建toast,但我的方法只包含一行代码。
我不确定这是否完全回答了你的问题,但到目前为止,我对Android (Java)编码的了解就这么多了。我希望这能帮到你。
https://stackoverflow.com/questions/9763458
复制相似问题