自定义Dialog且实现与Activity交互

自定义一个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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值