我有一个由ListView和TextView组成的布局。
在ListView项目上,它有一个Button。在Button的onClickListener上,是否能够更改父布局上TextView的文本?
我已经设置了Button的onClickListener,但是却找不到让Button更改TextView属性等的方法。
谢谢! :)
布局:
答案 0 :(得分:0)
// try this way,hope this will help you..
**XML** code
**activity**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical" >
<TextView
android:id="@+id/txtChnageTextColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Android Demo Text For TextView"/>
<ListView
android:id="@+id/lstChangeTextColor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1"/>
</LinearLayout>
**list_item**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/txtColor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"/>
<Button
android:id="@+id/btnSet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"/>
</LinearLayout>
**ACTIVITY** code
**MyActivity**
public class MyActivity extends Activity{
private ListView lstChangeTextColor;
private TextView txtChnageTextColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
lstChangeTextColor = (ListView) findViewById(R.id.lstChangeTextColor);
txtChnageTextColor = (TextView) findViewById(R.id.txtChnageTextColor);
ArrayList<HashMap<String,String>> listItem = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map1 = new HashMap<String, String>();
map1.put("name", "Red");
map1.put("value", "#FF0000");
HashMap<String,String> map2 = new HashMap<String, String>();
map2.put("name", "Orange");
map2.put("value", "#FFA500");
HashMap<String,String> map3 = new HashMap<String, String>();
map3.put("name", "Yellow");
map3.put("value", "#FFFF00");
HashMap<String,String> map4 = new HashMap<String, String>();
map4.put("name", "Lime");
map4.put("value", "#00FF00");
HashMap<String,String> map5 = new HashMap<String, String>();
map5.put("name", "Blue");
map5.put("value", "#0000FF");
listItem.add(map1);
listItem.add(map2);
listItem.add(map3);
listItem.add(map4);
listItem.add(map5);
lstChangeTextColor.setAdapter(new ListAdapter(this,listItem));
}
class ListAdapter extends BaseAdapter{
private ArrayList<HashMap<String,String>> listItem;
private Context context;
public ListAdapter (Context context,ArrayList<HashMap<String,String>> listItem) {
this.listItem = listItem;
this.context = context;
}
@Override
public int getCount() {
return listItem.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return listItem.get(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null,false);
holder.txtColor = (TextView) convertView.findViewById(R.id.txtColor);
holder.btnSet = (Button) convertView.findViewById(R.id.btnSet);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtColor.setText(listItem.get(position).get("name"));
holder.btnSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
@Override
public void run() {
txtChnageTextColor.setTextColor(Color.parseColor(listItem.get(position).get("value")));
}
});
}
});
return convertView;
}
}
class ViewHolder{
Button btnSet;
TextView txtColor;
}
}