自定义Dialog且实现与Activity交互
来源:互联网 发布:ubuntu istat 编辑:程序博客网 时间:2024/06/06 21:06
自定义一个Dialog,其中包含RadioGroup、RadioButton、EditText、Button。
Activity 给Dialog传入数据,Activity能获得Dialog的数据,实现了Activity与Dialog的交互。
1.xml
(1)activity_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" tools:context=".MainActivity" > <Button android:id="@+id/bt_show_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /></RelativeLayout>
(2)new_dialog.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="wrap_content" android:background="@color/white" android:paddingBottom="20dp" > <RelativeLayout android:id="@+id/firstRl" android:layout_width="match_parent" android:layout_height="40dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:text="@string/execution_of_doctors_order" android:textColor="@color/newblue" /> <TextView android:id="@+id/cancel_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:text="@string/close" android:textColor="@color/white" android:visibility="gone" /> </RelativeLayout> <View android:id="@+id/view" android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@id/firstRl" android:background="@color/gray" /> <TextView android:id="@+id/type_tv" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/view" android:layout_marginLeft="15dp" android:gravity="center_vertical" android:text="@string/please_select_type_of_doctors_order_execution" android:textColor="@color/newblue" /> <RadioGroup android:id="@+id/type_rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/type_tv" android:layout_marginLeft="15dp" android:orientation="horizontal" > <RadioButton android:id="@+id/puncture_rb" android:layout_width="90dp" android:layout_height="wrap_content" android:button="@drawable/radio_button" android:paddingLeft="10dp" android:text="@string/puncture" android:textColor="@color/newblue" /> <RadioButton android:id="@+id/patrol_rb" android:layout_width="90dp" android:layout_height="wrap_content" android:button="@drawable/radio_button" android:paddingLeft="10dp" android:text="@string/patrol" android:textColor="@color/newblue" /> <RadioButton android:id="@+id/withdrawal_of_needles_rb" android:layout_width="90dp" android:layout_height="wrap_content" android:button="@drawable/radio_button" android:paddingLeft="10dp" android:text="@string/withdrawal_of_needles" android:textColor="@color/newblue" /> </RadioGroup> <TextView android:id="@+id/exceptionalCase_tv" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/type_rg" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:gravity="center_vertical" android:text="@string/exceptional_case_exist_or_not" android:textColor="@color/newblue" /> <RadioGroup android:id="@+id/exceptionalCase_rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/exceptionalCase_tv" android:layout_marginLeft="15dp" android:orientation="horizontal" > <RadioButton android:id="@+id/nothave_rb" android:layout_width="100dp" android:layout_height="wrap_content" android:button="@drawable/radio_button" android:paddingLeft="10dp" android:text="@string/nothave" android:textColor="@color/newblue" /> <RadioButton android:id="@+id/have_rb" android:layout_width="100dp" android:layout_height="wrap_content" android:button="@drawable/radio_button" android:paddingLeft="10dp" android:text="@string/have" android:textColor="@color/newblue" /> </RadioGroup> <EditText android:id="@+id/exceptionalCase_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/exceptionalCase_rg" android:layout_margin="15dp" android:enabled="false" android:gravity="top|left" android:minHeight="100dp" android:padding="10dp" /> <RelativeLayout android:id="@+id/forthRl" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/exceptionalCase_et" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" > <Button android:id="@+id/save_btn" android:layout_width="120dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:background="@drawable/btnsearch" android:text="@string/save" android:textColor="@color/white" /> <Button android:id="@+id/close_btn" android:layout_width="120dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:background="@drawable/btnsearch" android:text="@string/close" android:textColor="@color/white" /> </RelativeLayout></RelativeLayout>
2.Java代码
(1)MainActivity.java
package com.example.demo_dialog0719;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity { Button showDialog; private NewDialog newDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); showDialog = (Button) findViewById(R.id.bt_show_dialog); showDialog.setOnClickListener(new OnClickListener() { String prescriptionGroupNum = "Hello World"; @Override public void onClick(View v) { newDialog = new NewDialog(MainActivity.this, prescriptionGroupNum) { @Override public void onSign(String prescriptionGroupNum, String type, String executePersonExceptional) { Toast.makeText(MainActivity.this, prescriptionGroupNum, Toast.LENGTH_LONG).show(); } }; } }); }}
(2)NewDialog.java
/* * @Title: NewDialog.java * @Description: TODO * @author: 张志安 * @data: 2016-7-19 下午3:09:59 * */package com.example.demo_dialog0719;import android.app.Dialog;import android.content.Context;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;import android.widget.RadioGroup.OnCheckedChangeListener;/** * TODO * * @author 张志安 * @data: 2016-7-19 下午3:09:59 */public abstract class NewDialog extends Dialog { // 事件类型 private RadioGroup type_rg; // 有无特殊情况 private RadioGroup exceptionalCase_rg; // 特殊情况 private EditText exceptionalCase_et; private RadioButton nothave_rb; private RadioButton have_rb; // 保存 private Button save_btn; // 关闭 private Button close_btn; // 选择事件类型后标记 private String type; public NewDialog(final Context context, final String prescriptionGroupNum) { super(context); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.new_dialog); type_rg = (RadioGroup) findViewById(R.id.type_rg); exceptionalCase_rg = (RadioGroup) findViewById(R.id.exceptionalCase_rg); nothave_rb = (RadioButton) findViewById(R.id.nothave_rb); have_rb = (RadioButton) findViewById(R.id.have_rb); exceptionalCase_et = (EditText) findViewById(R.id.exceptionalCase_et); save_btn = (Button) findViewById(R.id.save_btn); close_btn = (Button) findViewById(R.id.close_btn); type_rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (group.getCheckedRadioButtonId()) { case R.id.puncture_rb: type = "1"; nothave_rb.setClickable(false); have_rb.setClickable(false); nothave_rb.setChecked(false); have_rb.setChecked(false); exceptionalCase_et.setText(""); exceptionalCase_et.clearFocus(); exceptionalCase_et.setEnabled(false); break; case R.id.patrol_rb: type = "2"; nothave_rb.setClickable(true); have_rb.setClickable(true); nothave_rb.setChecked(true); have_rb.setChecked(false); break; case R.id.withdrawal_of_needles_rb: type = "3"; nothave_rb.setClickable(false); have_rb.setClickable(false); nothave_rb.setChecked(false); have_rb.setChecked(false); exceptionalCase_et.setText(""); exceptionalCase_et.clearFocus(); exceptionalCase_et.setEnabled(false); break; default: break; } } }); exceptionalCase_rg .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (group.getCheckedRadioButtonId()) { case R.id.nothave_rb: exceptionalCase_et.setText(""); exceptionalCase_et.clearFocus(); exceptionalCase_et.setEnabled(false); break; case R.id.have_rb: exceptionalCase_et.setEnabled(true); exceptionalCase_et.setFocusable(true); break; default: break; } } }); save_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (null == type || "".equals(type)) { Toast.makeText(context, "请选择事件类型", Toast.LENGTH_LONG) .show(); } else { onSign(prescriptionGroupNum, type, exceptionalCase_et .getText().toString()); } } }); close_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dismiss(); } }); setCancelable(false); show(); } public abstract void onSign(String prescriptionGroupNum, String type, String executePersonExceptional);}
0 0
- 自定义Dialog且实现与Activity交互
- Activity实现自定义Dialog
- Activity实现自定义Dialog
- 用Activity的dialog主题实现自定义dialog
- 自定义Dialog(二)之Dialog与Activity传值
- 自定义dialog-----activity
- Android 与Springmvc之间交互若干问题(消息,文件,自定义dialog...)
- Android 自定义Notification消息通知样式并实现与Activity简单交互
- Android自定义Dialog实现弹出它的Activity背景变暗,并自定义Dialog的大小
- fragment与activity实现数据交互
- 自定义dialog 占用整个activity
- Android透明背景Dialog、Dialog实现悬浮窗口 可与输入法交互,不被输入法覆盖
- Activity 自定义dialog (自定义layout)
- 自定义Dialog实现
- Dialog-自定义实现
- 自定义dialog的实现
- 自定义Dialog实现方式
- 自定义Dialog实现
- 通过按钮复制一段代码
- 9. Palindrome Number
- 向 Activity 添加片段
- 启动和停止SQL server
- CodeForces 347B Fixed Points
- 自定义Dialog且实现与Activity交互
- 使用Devstack部署neutron网络节点
- hdu 2577
- Android中Surface和SurfaceView的一些理解和总结
- Android逆向分析案例——某酒店APP的登陆请求分析
- Machine Learning week3
- MySQL数据库,根据传入的字符换进行切割,并获得指定位置被切割的字符串。
- source insight中如何可以添加makefile,.S 等这类型的文件?
- 190. Reverse Bits