安卓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。

时间: 2024-08-24 19:51:22

安卓ViewPager中Button点击事件弹出Dialog的相关文章

Android GridView中Button点击事件onItemClick不能响应

今天在捣鼓一个类似于百度贴吧的东西.布局:上面是个ActionBar标题栏,然后是一个GridView布局,在Java代码中动态加载关注的贴吧,一切就绪中,很愉快的弄好了! 现在需要点击选项进入某个贴吧,那么问题来了—— GridView中Button点击事件onItemClick不能响应. 所以,主要的猫腻还是在com.android.internal.R.attr.buttonStyle这个里面,查看这个xml文件,Button设置多了两个属性如下:<item name="androi

Android实战简易教程-第十五枪(实现ListView中Button点击事件监听)

1.main.xml <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" androi

Android学习之解决ListView中item点击事件和item中Button点击事件冲突问题

在ListView中添加Button后,如果只是单纯的加入而不加限制的话,ListView的onClick点击事件没有响应,因为Button获取了item的焦点,想要两者都可点击,需要加上如下限制: 在ListView的适配器中的布局文件中添加: (1)在布局文件的根元素上中添加属性android:descendantFocusability="blocksDescendants" (2)在Button中添加属性android:focusable="false"和a

element-ui 点击编辑弹出dialog组件中select组件绑定值改变,但是不触发change事件

代码结构如下: 现象视频如下: 现象原因:经过排查发现 此时点击操作不触发chang事件,后台响应数据中没有订单取消原因orderCanleRemark字段,此时导致不触发change事件, 解决方案:方案1,:让后台配合响应该字段,无论是否为空都响应该字段 方案2:在后台响应数据赋值给,this.form之前,手动添加该字段到后台响应数据中 代码如下: 原文地址:https://www.cnblogs.com/liujiazhu/p/8649810.html

从Listview与Button点击事件冲突看安卓点击事件分发机制

题目有点长.其实实现Listview的时候大家都可能会碰到这样的一个问题,那就是Listview的OnItemClickListener点击事件与Button(或者checkbox)的touch(或者click)事件冲突的问题. 声明一下,非常感谢郭大师的这篇blog: http://blog.csdn.net/guolin_blog/article/details/9097463 原理参考了这篇blog,事实上也是本人功力不够不能阅读源码的缺陷啊. 下面说下自己的解决步骤: 1)首先先set一

关于在&quot;a&quot;标签中添加点击事件的一些问题

昨天做修改页面跳转时遇到一个问题,如果a标签的"href"属性为空的话,比如这样<a href="" onclick="roleupdate()">修改</a>,这时当我点击修改链接时并没有给我跳到对应修改页面,而是只在本页面进行了刷新操作:如果写成<input type="button" onclick="roledelete()" value="修改"&

Android笔记——Button点击事件几种写法

Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button的onClick的属性 首先我们简单地定义一个带Button的xml布局文件 activity_main.xml: <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:

[转]iOS Safari 中click点击事件失效的解决办法

iOS Safari 中click点击事件失效的解决办法 问题起因: 在微信公众号开发(微站)过程中用jquery的live方法绑定的click事件点击无效(不能执行) 问题描述 当使用委托给一个元素添加click事件时,如果事件是委托到 document 或 body 上,并且委托的元素是默认不可点击的(如 div, span 等),此时 click 事件会失效. 解决办法 解决办法有 4 种可供选择: ?将 click 事件直接绑定到目标?元素(??即 .target)上 将目标?元素换成 

CorePlot学习六---点击scatterPlot中的symbol点时弹出相应的注释

由于项目需要用到用户点击 symbol时,弹出相应的详细信息,发现国内讲解的比较少,经过一番搜索验证终于解决,先看效果图: 具体需要修改的代码如下: 首先要引用委托方法:CPTScatterPlotDelegate.CPTPlotSpaceDelegate 完成如下: #pragma mark - #pragma mark CPTPlotSpaceDelegate methods -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointin