我有一个问题。我想使用RIGHT DPAD按钮更改TextView中的文本(此TextView在ListView中)。我已经研究过几个首先是setOnClickListener
的解决方案。我猜setOnClickListener
不能用于键码,因为这个侦听器是用于触摸模式的。我也研究过使用setOnItemClickListener
。根据我的理解,这个听众只在按下DPAD CENTER时才有效吗?最后使用我不太熟悉的setOnKeyListener
。我在下面准备一些代码。有谁能告诉我我的代码有什么问题?
java代码:
public class ListViewExampleActivity extends Activity {
String[] items = { "a", "b", "c", "d" };
String[] itemsParameters = { "1", "2", "3", "4" };
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
}
private void populateListView() {
CustomList adapter = new CustomList(this, items, itemsParameters);
list = (ListView) findViewById(R.id.ListView);
list.setAdapter(adapter);
}
private class CustomList extends BaseAdapter {
private final Activity context;
private final String[] items;
private final String[] itemsParameters;
public CustomList(Activity context, String[] items, String[] itemsParameters) {
super();
this.context = context;
this.items = items;
this.itemsParameters = itemsParameters;
}
public class Holder {
TextView txtitems;
TextView txtitemsParameters;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
LayoutInflater layoutinflater = context.getLayoutInflater();
View rowView = null;
rowView = layoutinflater.inflate(R.layout.text_views_1, null, true);
holder.txtitems = (TextView) rowView.findViewById(R.id.items);
holder.txtitemsParameters = (TextView) rowView.findViewById(R.id.itemsParameters);
holder.txtitems.setText(items[position]);
rowView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
holder.txtitemsParameters.setText(itemsParameters[position]);
return true;
}
}
return false;
}
});
return rowView;
}
@Override
public int getCount() {
return 4;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
}
}
activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:baselineAligned="true"
android:orientation="vertical"
tools:context="com.example.publicmodeexample.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:divider="@color/black"
android:gravity="center"
android:text="LIST VIEW"
android:textColor="@color/white" />
<ListView
android:id="@+id/ListView"
android:layout_width="318dp"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:divider="@color/black"
android:dividerHeight="1dp"
android:listSelector="@drawable/text_color_change" >
</ListView>
</LinearLayout>
</LinearLayout>
text_views_1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TextView
android:id="@+id/items"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF" />
<TextView
android:id="@+id/itemsParameterOpenBracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="["
android:textColor="#FFFFFFFF" />
<TextView
android:id="@+id/itemsParameter"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:gravity="end"
android:textColor="#FFFFFFFF"/>
<TextView
android:id="@+id/itemsParameterCloseBracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="]"
android:textColor="#FFFFFFFF" />
</LinearLayout>
@绘制/ text_color_change:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/custom_border" android:state_focused="true"/>
</selector>
我是否需要添加focusable
属性?我很喜欢这家酒店。