自定义Dialog回传数据给Activity

在Activity里点击弹出Dialog,当选取需要的值之后,把这个值传回给Activity。

大概思路:

1、自定义对话框,并继承Dialog ;
2、在自定义对话框中,定义一个接口,并声明一个方法,将所要传递的参数作为方法参数 ;
3、在activity中,创建自定义对话框类,并调用自定义的接口,获取操作结果并更新UI。


废话少说,我们演示例子:


源码MainActivity:

public class MainActivity extends ActionBarActivity implements OnClickListener {
	private Button tv_dialog;
	private Context context = MainActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_dialog = (Button) findViewById(R.id.tv_dialog);
        tv_dialog.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_dialog:
			//显示Dialog
			MyDialog myDialog = new MyDialog(this,R.style.Dialog,new MyDialog.PriorityListener() {
				@Override
				public void setActivityText(String string) {
					tv_dialog.setText(string);
				}
			});
			myDialog.show();
			break;

		}
	}

}

activity_mamin.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="com.itman.dialogdemo.MainActivity" >

    <Button
        android:id="@+id/tv_dialog"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="点击弹出Dialog" />

</RelativeLayout>


接下来是自定义的Dialog:

public class MyDialog extends Dialog {
	private Context context;
	private Button bt_change;
	private TextView tv_value;

	/**
	 * 自定义Dialog监听器
	 */
	public interface PriorityListener {
		/**
		 * 回调函数,用于在Dialog的监听事件触发后刷新Activity的UI显示
		 */
		void setActivityText(String string);
	}

	private PriorityListener listener;

	public MyDialog(Context context, int theme, PriorityListener listener) {
		super(context, theme);
		this.context = context;
		this.listener = listener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LayoutInflater inflate = LayoutInflater.from(context);
		View view = inflate.inflate(R.layout.dialog_show, null);
		setContentView(view);

		//设置Dialog的大小
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.8); // 宽度设置为屏幕的0.8
		dialogWindow.setAttributes(lp);

		bt_change = (Button) view.findViewById(R.id.bt_change);
		tv_value = (TextView) view.findViewById(R.id.tv_value);

		bt_change.setOnClickListener(new AdapterView.OnClickListener() {

			@Override
			public void onClick(View v) {
				listener.setActivityText(tv_value.getText().toString());
				dismiss();
			}
		});

	}

}


最后是Dialog样式:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/bt_change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="点击传回下面的值" />

    <TextView
        android:id="@+id/tv_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/bt_change"
        android:layout_below="@+id/bt_change"
        android:layout_marginTop="48dp"
        android:text="我是Dialog里面的值" />

</RelativeLayout>

Demo完成,直接运行就可以。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值