自定义Dialog且实现与Activity交互
2016-07-19 15:43
459 查看
自定义一个Dialog,其中包含RadioGroup、RadioButton、EditText、Button。
Activity 给Dialog传入数据,Activity能获得Dialog的数据,实现了Activity与Dialog的交互。
1.xml
(1)activity_main.xml
(2)new_dialog.xml
2.Java代码
(1)MainActivity.java
(2)NewDialog.java
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); }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories