淘先锋技术网

首页 1 2 3 4 5 6 7

原因是button强制获取了item的焦点,只要设置button的focusable为false即可。



  1 import android.app.Activity;
  2 import android.app.AlertDialog;
  3 import android.content.Context;
  4 import android.os.Bundle;
  5 import android.view.LayoutInflater;
  6 import android.view.Menu;
  7 import android.view.View;
  8 import android.view.ViewGroup;
  9 import android.widget.AdapterView;
 10 import android.widget.BaseAdapter;
 11 import android.widget.Button;
 12 import android.widget.ImageView;
 13 import android.widget.ListView;
 14 import android.widget.TextView;
 15 import android.widget.Toast;
 16 
 17 public class Audition1 extends Activity {
 18 
 19     private ListView listView;
 20 
 21     @Override
 22     public void onCreate(Bundle savedInstanceState) {
 23         super.onCreate(savedInstanceState);
 24         setContentView(R.layout.activity_audition1);
 25         initComponents();
 26 
 27     }
 28 
 29     private void initComponents() {
 30         listView = (ListView) findViewById(R.id.listView);
 31         listView.setAdapter(new MyAdapter(this));
 32         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 33 
 34             @Override
 35             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
 36                     long arg3) {
 37                 Toast.makeText(getApplicationContext(), "item", 300).show();
 38 
 39             }
 40         });
 41     }
 42 
 43     @Override
 44     public boolean onCreateOptionsMenu(Menu menu) {
 45         getMenuInflater().inflate(R.menu.activity_audition1, menu);
 46         return true;
 47     }
 48 
 49     public final class ViewHolder {
 50         public TextView textView;
 51         public Button button;
 52         public ImageView imageView;
 53     }
 54 
 55     class MyAdapter extends BaseAdapter {
 56 
 57         private LayoutInflater mInflater;
 58 
 59         public MyAdapter(Context context) {
 60             this.mInflater = LayoutInflater.from(context);
 61         }
 62 
 63         @Override
 64         public int getCount() {
 65             return 3;
 66         }
 67 
 68         @Override
 69         public Object getItem(int position) {
 70             return null;
 71         }
 72 
 73         @Override
 74         public long getItemId(int position) {
 75             return position;
 76         }
 77 
 78         @Override
 79         public View getView(int position, View convertView, ViewGroup parent) {
 80             ViewHolder holder = null;
 81             if (convertView == null) {
 82                 holder = new ViewHolder();
 83 
 84                 convertView = mInflater.inflate(R.layout.item, null);
 85                 holder.imageView = (ImageView) convertView
 86                         .findViewById(R.id.imageView);
 87                 holder.textView = (TextView) convertView
 88                         .findViewById(R.id.textViewId);
 89                 holder.button = (Button) convertView.findViewById(R.id.button);
 90                 holder.textView.setText("shit");
 91                 holder.button.setOnClickListener(new View.OnClickListener() {
 92 
 93                     @Override
 94                     public void onClick(View v) {
 95                         AlertDialog.Builder builder = new AlertDialog.Builder(
 96                                 Audition1.this);
 97                         builder.setMessage("dialog");
 98                         builder.setTitle("title");
 99                         builder.create();
100                         builder.show();
101                     }
102                 });
103                 convertView.setTag(holder);
104             } else {
105                 convertView.getTag();
106             }
107 
108             return convertView;
109         }
110 
111     }
112 
113 }
复制代码

 

 

复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="@dimen/padding_medium"
        tools:context=".Audition1" 
        android:dividerHeight="5dp"/>

</RelativeLayout>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "fill_parent"
     android:layout_height= "match_parent"
     android:orientation= "horizontal" >
 
     <TextView
         android:id= "@+id/textViewId"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "@string/hello_world"
         />
     <ImageView
         android:id= "@+id/imageView"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:src= "@drawable/ic_launcher"
         android:contentDescription= "@string/app_name"
         />
 
     <Button
         android:id= "@+id/button"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "press"
         android:focusable= "false"
         />
 
</LinearLayout>

  1 import android.app.Activity;
  2 import android.app.AlertDialog;
  3 import android.content.Context;
  4 import android.os.Bundle;
  5 import android.view.LayoutInflater;
  6 import android.view.Menu;
  7 import android.view.View;
  8 import android.view.ViewGroup;
  9 import android.widget.AdapterView;
 10 import android.widget.BaseAdapter;
 11 import android.widget.Button;
 12 import android.widget.ImageView;
 13 import android.widget.ListView;
 14 import android.widget.TextView;
 15 import android.widget.Toast;
 16 
 17 public class Audition1 extends Activity {
 18 
 19     private ListView listView;
 20 
 21     @Override
 22     public void onCreate(Bundle savedInstanceState) {
 23         super.onCreate(savedInstanceState);
 24         setContentView(R.layout.activity_audition1);
 25         initComponents();
 26 
 27     }
 28 
 29     private void initComponents() {
 30         listView = (ListView) findViewById(R.id.listView);
 31         listView.setAdapter(new MyAdapter(this));
 32         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 33 
 34             @Override
 35             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
 36                     long arg3) {
 37                 Toast.makeText(getApplicationContext(), "item", 300).show();
 38 
 39             }
 40         });
 41     }
 42 
 43     @Override
 44     public boolean onCreateOptionsMenu(Menu menu) {
 45         getMenuInflater().inflate(R.menu.activity_audition1, menu);
 46         return true;
 47     }
 48 
 49     public final class ViewHolder {
 50         public TextView textView;
 51         public Button button;
 52         public ImageView imageView;
 53     }
 54 
 55     class MyAdapter extends BaseAdapter {
 56 
 57         private LayoutInflater mInflater;
 58 
 59         public MyAdapter(Context context) {
 60             this.mInflater = LayoutInflater.from(context);
 61         }
 62 
 63         @Override
 64         public int getCount() {
 65             return 3;
 66         }
 67 
 68         @Override
 69         public Object getItem(int position) {
 70             return null;
 71         }
 72 
 73         @Override
 74         public long getItemId(int position) {
 75             return position;
 76         }
 77 
 78         @Override
 79         public View getView(int position, View convertView, ViewGroup parent) {
 80             ViewHolder holder = null;
 81             if (convertView == null) {
 82                 holder = new ViewHolder();
 83 
 84                 convertView = mInflater.inflate(R.layout.item, null);
 85                 holder.imageView = (ImageView) convertView
 86                         .findViewById(R.id.imageView);
 87                 holder.textView = (TextView) convertView
 88                         .findViewById(R.id.textViewId);
 89                 holder.button = (Button) convertView.findViewById(R.id.button);
 90                 holder.textView.setText("shit");
 91                 holder.button.setOnClickListener(new View.OnClickListener() {
 92 
 93                     @Override
 94                     public void onClick(View v) {
 95                         AlertDialog.Builder builder = new AlertDialog.Builder(
 96                                 Audition1.this);
 97                         builder.setMessage("dialog");
 98                         builder.setTitle("title");
 99                         builder.create();
100                         builder.show();
101                     }
102                 });
103                 convertView.setTag(holder);
104             } else {
105                 convertView.getTag();
106             }
107 
108             return convertView;
109         }
110 
111     }
112 
113 }
复制代码

 

 

复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="@dimen/padding_medium"
        tools:context=".Audition1" 
        android:dividerHeight="5dp"/>

</RelativeLayout>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "fill_parent"
     android:layout_height= "match_parent"
     android:orientation= "horizontal" >
 
     <TextView
         android:id= "@+id/textViewId"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "@string/hello_world"
         />
     <ImageView
         android:id= "@+id/imageView"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:src= "@drawable/ic_launcher"
         android:contentDescription= "@string/app_name"
         />
 
     <Button
         android:id= "@+id/button"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "press"
         android:focusable= "false"
         />
 
</LinearLayout>