安卓ViewPager中Button点击事件弹出Dialog

首先页面采用ViewPager,在第一个页面中有一个按钮,要求点击这个页面上的按钮弹出一个对话框。

先贴出效果图:

分析难点:1、在ViewPager中,共有四个选项卡页,找到第一个页面中Button按钮。先贴出代码:

  1 package com.example.acountrecorder;
  2 import java.util.ArrayList;
  3 import java.util.HashMap;
  4 import java.util.List;
  5 import com.example.adapter.MyPagerAdapter;
  6 import com.example.entity.DishesMenu;
  7 import com.example.service.MenuService;
  8 import android.app.Activity;
  9 import android.app.AlertDialog;
 10 import android.content.DialogInterface;
 11 import android.os.Bundle;
 12 import android.support.v4.view.ViewPager;
 13 import android.support.v4.view.ViewPager.OnPageChangeListener;
 14 import android.view.LayoutInflater;
 15 import android.view.Menu;
 16 import android.view.View;
 17 import android.view.View.OnClickListener;
 18 import android.view.ViewGroup;
 19 import android.widget.Button;
 20 import android.widget.ListView;
 21 import android.widget.RadioButton;
 22 import android.widget.RadioGroup;
 23 import android.widget.SimpleAdapter;
 24 import android.widget.RadioGroup.OnCheckedChangeListener;
 25 import android.widget.Toast;
 26 
 27 public class MainActivity extends Activity {
 28     
 29     private Button btnMoney,adddish;
 30     private RadioButton dishes,choosedishbyhand,choosedishrandom,payrecorder;
 31     private RadioGroup radioGroup;
 32     private ListView listView;
 33     private SimpleAdapter simpleAdapter;
 34     private ViewPager vpager;
 35     private int currIndex=0;
 36     private ArrayList<View> listViews;
 37     
 38     @Override
 39     protected void onCreate(Bundle savedInstanceState) {
 40         super.onCreate(savedInstanceState);
 41         setContentView(R.layout.activity_main);
 42         
 43         init();
 44         InitViewPager();
 45         doSthRelative();
 46         setAction();
 47     }
 48 
 49     private void setAction() {
 50         if (currIndex==0) {
 51             //下面两句诗关键!!!!
 52             View view1 = listViews.get(currIndex);
 53             adddish = (Button) view1.findViewById(R.id.adddish);
 54             adddish.setOnClickListener(new OnClickListener() {
 55                 @Override
 56                 public void onClick(View v) {
 57                     LayoutInflater inflater = getLayoutInflater();
 58                     View layout = inflater.inflate(R.layout.dialog_adddish,
 59                          (ViewGroup) findViewById(R.id.dialog_dish));
 60                        
 61                     new AlertDialog.Builder(MainActivity.this).setTitle("").setView(layout).setPositiveButton("确定",
 62                             new DialogInterface.OnClickListener() {
 63                                 @Override
 64                                 public void onClick(DialogInterface dialog, int which) {
 65                                     // TODO Auto-generated method stub
 66                                     Toast.makeText(MainActivity.this, "dialog弹出来的", Toast.LENGTH_SHORT).show();
 67                                 }
 68                             }
 69                         
 70                     ).setNegativeButton("取消", null).show();   
 71                 }
 72                 
 73             });
 74         }
 75         
 76     }
 77 
 78     private void init() {
 79         btnMoney = (Button) findViewById(R.id.btn_money);
 80         radioGroup = (RadioGroup) findViewById(R.id.radiogroup_item);
 81         dishes = (RadioButton) findViewById(R.id.disheschoice);
 82         choosedishbyhand = (RadioButton) findViewById(R.id.choosedishbyhand); 
 83         choosedishrandom = (RadioButton) findViewById(R.id.choosedishrandom);
 84         payrecorder = (RadioButton) findViewById(R.id.paymentrecord);
 85         //listView = (ListView) findViewById(R.id.checkdishes);
 86         radioGroup.setOnCheckedChangeListener(rdgcc);
 87         vpager = (ViewPager) findViewById(R.id.vPager);
 88     }
 89 
 90     private RadioGroup.OnCheckedChangeListener rdgcc = new OnCheckedChangeListener() {
 91              
 92             @Override
 93             public void onCheckedChanged(RadioGroup group, int checkedId) {
 94                 int r1 = dishes.getId(),r2 = choosedishbyhand.getId(),r3 = choosedishrandom.getId(),r4 = payrecorder.getId();
 95                 if (r1==checkedId) {
 96                     Toast.makeText(MainActivity.this, dishes.getText().toString(), Toast.LENGTH_SHORT).show();
 97                     currIndex = 0;
 98                     vpager.setCurrentItem(currIndex);//加载完页面后再加载页面上的元素
 99                     //adddish = (Button) findViewById(R.id.adddish);
100                 }else if (r2==checkedId) {
101                     Toast.makeText(MainActivity.this, choosedishbyhand.getText().toString(), Toast.LENGTH_SHORT).show();
102                     currIndex = 1;
103                     vpager.setCurrentItem(currIndex);
104                 }else if (r3==checkedId) {
105                     Toast.makeText(MainActivity.this, choosedishrandom.getText().toString(), Toast.LENGTH_SHORT).show();
106                     currIndex = 2;
107                     vpager.setCurrentItem(currIndex);
108                 }else if (r4==checkedId) {
109                     Toast.makeText(MainActivity.this, payrecorder.getText().toString(), Toast.LENGTH_SHORT).show();
110                     currIndex = 3;
111                     vpager.setCurrentItem(currIndex);
112                 }    
113             }
114         };
115 
116         private void InitViewPager() {
117             vpager = (ViewPager) findViewById(R.id.vPager);
118             listViews = new ArrayList<View>();
119             LayoutInflater mInflater = getLayoutInflater();
120             listViews.add(mInflater.inflate(R.layout.mydishes, null));
121             listViews.add(mInflater.inflate(R.layout.chosdishhandly, null));
122             listViews.add(mInflater.inflate(R.layout.chosdishrandom, null));
123             listViews.add(mInflater.inflate(R.layout.recorders, null));
124             vpager.setAdapter(new MyPagerAdapter(listViews));
125             vpager.setCurrentItem(0);
126             vpager.setOnPageChangeListener(new OnPageChangeListener() {
127                 @Override
128                 public void onPageSelected(int arg0) {
129                     switch (arg0) {
130                     case 0:
131                         dishes.setChecked(true);
132                         break;
133                     case 1:
134                         choosedishbyhand.setChecked(true);
135                         break;
136                     case 2:
137                         choosedishrandom.setChecked(true);
138                         break;
139                     case 3:
140                         payrecorder.setChecked(true);
141                         break;
142                         
143                     default:
144                         break;
145                     }
146                 }
147                 @Override
148                 public void onPageScrolled(int arg0, float arg1, int arg2) {
149                 }
150                 @Override
151                 public void onPageScrollStateChanged(int arg0) {
152                 }
153             });
154         }    
155     private void doSthRelative(){
156         if (currIndex==0) {
157             //首先查询数据库中是否有数据,有数据则加载数据
158             listView = (ListView)findViewById(R.id.allmydishes);
159             ArrayList<HashMap<String, Object>> listMap = new ArrayList<HashMap<String, Object>>();
160             DishesMenu dish = new DishesMenu();
161             MenuService menuService = new MenuService(MainActivity.this);
162             List<DishesMenu> listMenu = menuService.queryAll();
163             if (listMenu!=null) {
164                 for (int i = 0; i < listMenu.size(); i++) {
165                      dish = listMenu.get(i);
166                      HashMap<String, Object> map = new HashMap<String, Object>();
167                      map.put("dishname", dish.getDishname());
168                      map.put("dishprice", dish.getDishprice());
169                      listMap.add(map);
170                 }
171                 simpleAdapter = new SimpleAdapter(this,listMap, R.layout.checkdisheslistviewitem, new String[]{"dishname","dishprice"}, new int[]{R.id.display_dishname,R.id.display_dishprice});
172                 listView.setAdapter(simpleAdapter);
173             }
174             
175         }
176         
177     }    
178     @Override
179     public boolean onCreateOptionsMenu(Menu menu) {
180         // Inflate the menu; this adds items to the action bar if it is present.
181         getMenuInflater().inflate(R.menu.main, menu);
182         return true;
183     }
184 
185 }

其次是activitymain.xml代码:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:background="#FFFFFFFF" >
 7 
 8     <RelativeLayout
 9         android:layout_width="match_parent"
10         android:layout_height="50dip"
11         android:background="#FF333333" >
12         <TextView
13                 android:layout_width="wrap_content"
14                 android:layout_height="wrap_content"
15                 android:layout_centerInParent="true"
16                 android:text="@string/title"
17                 android:textColor="#FFFFFFFF"
18                 android:textSize="20sp" />
19         <Button
20                 android:layout_width="40dip"
21                 android:layout_height="40dip"
22                 android:id="@+id/btn_money"
23                 android:layout_centerInParent="true"
24                 android:layout_alignParentRight="true"
25                 android:layout_marginRight="20dip"
26                 android:background="@drawable/moneypackage" />
27     </RelativeLayout>
28     
29         <RadioGroup 
30             android:id="@+id/radiogroup_item"
31             android:layout_width="wrap_content"
32             android:layout_height="40dip"
33             android:orientation="horizontal">
34             <RadioButton android:id="@+id/disheschoice"
35                 android:layout_width="wrap_content"
36                 android:layout_height="40dip"
37                 android:layout_weight="1"
38                 android:background="@drawable/rb_bg"
39                 android:button="@null"
40                 android:checked="true"
41                 android:gravity="center"
42                 android:text="@string/dishestochoose"
43                 android:textColor="#FFFFFFFF"
44                 android:textSize="18sp"
45                 android:textStyle="bold"
46                 />
47             <RadioButton android:id="@+id/choosedishbyhand"
48                 android:layout_width="wrap_content"
49                 android:layout_height="40dip"
50                 android:layout_weight="1"
51                 android:background="@drawable/rb_bg"
52                 android:button="@null"
53                 android:checked="false"
54                 android:gravity="center"
55                 android:text="@string/choosedishbyhand"
56                 android:textColor="#FFFFFFFF"
57                 android:textSize="18sp"
58                 android:textStyle="bold"
59                 />
60              <RadioButton android:id="@+id/choosedishrandom"
61                 android:layout_width="wrap_content"
62                 android:layout_height="40dip"
63                 android:layout_weight="1"
64                 android:background="@drawable/rb_bg"
65                 android:button="@null"
66                 android:checked="false"
67                 android:gravity="center"
68                 android:text="@string/choosedishrandom"
69                 android:textColor="#FFFFFFFF"
70                 android:textSize="18sp"
71                 android:textStyle="bold"
72                 />
73              <RadioButton android:id="@+id/paymentrecord"
74                 android:layout_width="wrap_content"
75                 android:layout_height="40dip"
76                 android:layout_weight="1"
77                 android:background="@drawable/rb_bg"
78                 android:button="@null"
79                 android:checked="false"
80                 android:gravity="center"
81                 android:text="@string/paymentrecord"
82                 android:textColor="#FFFFFFFF"
83                 android:textSize="18sp"
84                 android:textStyle="bold"
85                 />   
86         </RadioGroup>
87         <android.support.v4.view.ViewPager
88             android:layout_width="wrap_content"
89             android:layout_height="wrap_content"
90             android:id="@+id/vPager"
91             android:layout_gravity="center"
92             android:background="#000000"
93             android:flipInterval="30"
94             android:persistentDrawingCache="animation"
95             />
96         
97 </LinearLayout>

第一个选项卡页面:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:background="#FFFF1565"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     tools:context=".MainActivity" >
 9     
10         
11         <ListView 
12             android:layout_width="match_parent"
13             android:layout_height="340dip"
14             android:id="@+id/allmydishes"
15             android:layout_marginTop="10dip"
16             >
17             
18         </ListView>
19         <Button 
20             android:layout_width="50dip"
21             android:layout_height="50dip"
22             android:background="@drawable/btn_add"
23             android:gravity="bottom"
24             android:id="@+id/adddish"
25             android:layout_marginTop="20dip"
26             android:layout_marginLeft="40dip"
27             />
28 </LinearLayout>

还有dialog.xml页面:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:id="@+id/dialog_dish"
 7     android:background="#00000000" >
 8     
 9     <LinearLayout 
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:layout_marginLeft="10dip"
13         android:orientation="horizontal">
14         <TextView 
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:text="@string/diahname"
18             android:textColor="#FFFFFFFF"
19             android:textSize="16sp">
20         </TextView>
21         <EditText 
22             android:layout_width="200dip"
23             android:layout_height="wrap_content"
24             android:layout_marginLeft="10dip"
25             android:inputType="text"
26             android:textSize="16sp"
27             android:id="@+id/dish_addnewname"/>
28     </LinearLayout>    
29     <LinearLayout 
30         android:layout_width="match_parent"
31         android:layout_height="wrap_content"
32         android:layout_marginLeft="10dip"
33         android:orientation="horizontal">
34         <TextView 
35             android:layout_width="wrap_content"
36             android:layout_height="wrap_content"
37             android:text="@string/diahprice"
38             android:textColor="#FFFFFFFF"
39             android:textSize="16sp">
40         </TextView>
41         <EditText 
42             android:layout_width="200dip"
43             android:layout_height="wrap_content"
44             android:layout_marginLeft="10dip"
45             android:inputType="text"
46             android:textSize="16sp"
47             android:id="@+id/dish_addnewprice"/>
48     </LinearLayout>   
49 </LinearLayout>

难点分析:2、AlertDialog的用法,详细查看AndroidAPI。

原文地址:https://www.cnblogs.com/SeawinLong/p/3987668.html