我想点击ListView项目内的按钮,点击整个项目应该具有相同的效果。但是,当我点击按钮时没有任何反应,当我点击图像时所有的Okey。怎么解决? 我尝试按下按钮: 机器人:可聚焦=“真” 机器人:可点击=“真” 我还尝试了android:descendantFocusability。
我的尝试都没有让按钮可以点击。
my_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:descendantFocusability="blocksDescendants"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_toLeftOf="@+id/imageArrow"
android:layout_toStartOf="@+id/imageArrow"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageArrow"
android:layout_alignTop="@+id/button"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@drawable/r_arr" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.listView);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
MyCustomAdapter adapter = new MyCustomAdapter(this, values);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_SHORT).show();
}
}
MyCustomAdapter.java
public class MyCustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
static class ViewHolder {
public TextView myButton;
public ImageView image;
}
public MyCustomAdapter(Activity context, String[] names) {
super(context, R.layout.my_item, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.my_item, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.myButton = (Button)rowView.findViewById(R.id.button);
viewHolder.image = (ImageView) rowView.findViewById(R.id.imageArrow);
viewHolder.myButton.setFocusable(false);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
String abs=Integer.toString(position + 1);
holder.myButton.setText(s+ " number " + abs);
holder.image.setImageResource(R.drawable.r_arr);
return rowView;
}
}
答案 0 :(得分:0)
你只需要配置像这样的onClick方法:
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//your code goes here
}
});
答案 1 :(得分:0)
请在按钮上写上onclicklistener。
答案 2 :(得分:0)
在getView中放置按钮单击侦听器
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.my_item, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.myButton = (Button) rowView.findViewById(R.id.button);
viewHolder.image = (ImageView) rowView.findViewById(R.id.imageArrow);
viewHolder.myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Button click functionalty here
});
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
String abs=Integer.toString(position + 1);
holder.myButton.setText(s+ " number " + abs);
holder.image.setImageResource(R.drawable.r_arr);
return rowView;
}
}
要使listview项和按钮可单击,请执行此操作。
使ListView可聚焦 android:focusable =&#34; true&#34;
按钮无法在您的自定义列表视图项目中聚焦 android:focusable =&#34; false&#34;
答案 3 :(得分:0)
方法1:
首先在xml中替换按钮代码,如下所示
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="false"
android:onClick="onClick"
android:focusableInTouchMode="false"
android:layout_toLeftOf="@+id/imageArrow"
android:layout_toStartOf="@+id/imageArrow"
/>
然后转到您的活动并编写如下代码
public void onClick(View v)
{
super.onClick(v);
if (v.getId() == R.id.button)
{
//your code goes here
}
}
或者你可以在下面添加以下代码和平创建
方法2:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
答案 4 :(得分:0)
不建议在listview中为同一行项目提供两个单击侦听器。
或