gpt4 book ai didi

android - 单击 ListView 项时如何更改按钮背景

转载 作者:行者123 更新时间:2023-11-29 16:14:54 27 4
gpt4 key购买 nike

我有一个 ListView ,并使用包含 1 个 ImageView、2 个 TextView 和 1 个按钮的自定义布局对其进行扩充。我想在单击 ListView 项时更改按钮背景,但无法执行。有人能帮助我吗?非常感谢您。

enter image description here

row_segnalibro.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="50dp" >

<ImageView android:id="@+id/ImageView01" android:src="@drawable/star"
android:layout_height="40dp"
android:layout_width="40dp"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp" />

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout02"
android:orientation="vertical"
android:layout_width="220dp" android:layout_height="match_parent">

<TextView
android:id="@+id/tvRow1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:textColor="#0967AD"
android:textStyle="bold"
/>
<TextView
android:id="@+id/tvRow2"
android:layout_below="@id/tvRow1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp"
android:textColor="#0967AD"
android:textStyle="bold"
/>
</RelativeLayout>
<Button
android:id="@+id/butt_segnalib"
android:background="@drawable/freccia_o"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center" />

Segnalibro.java

  public class Segnalibro extends ListActivity{
super.onCreate(savedInstanceState);
setContentView(R.layout.segnalibro);

lv = (ListView) getListView();
.....some code
MySimpleCursorAdap myadap = new MySimpleCursorAdap(getApplicationContext(),R.layout.rowlayout_segnalibro,curr,campi, to);
lv.setAdapter(myadap);
lv.setClickable(true);

lv.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,int position, long arg3) {
Log.i("","I have clicked on item "+position);

}
});

MySimpleCursorAdap.java

      public class MySimpleCursorAdap extends SimpleCursorAdapter{

public MySimpleCursorAdap(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mLayout = layout;
this.curr = c;
this.cont = context;
}

public void bindView(View view, Context context, final Cursor cursor) {

....some code

/* Button butt = (Button) view.findViewById(R.id.butt_segnalib);
butt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
.....code to change the background
}
}); */
}

在 MySimpleCursorAdap 中,我可以使用 findViewById 方法获取 Button,如何在 Segnalibro 类中做到这一点?

最佳答案

好的..所以基本上你需要一个状态列表来实现它..见this知道如何创建它..然后将其设置为按钮的背景..

关于android - 单击 ListView 项时如何更改按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10244237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com