【安卓知识点汇总】Dialog和Toast封装

这里自定义:

  • CustomDialog
public class CustomDialog {
	private AlertDialog.Builder builder;
	private Context context;

	public CustomDialog(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public void createDialog(String buttontext, String title, String message,
			final CallBack callBack) {
		builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(buttontext, new OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				callBack.isConfirm(true);
			}
		});
		builder.create().show();
	}

	public interface CallBack {
		public void isConfirm(boolean flag);
	}

	public void createToasts(String message,LayoutInflater layoutInflater) {
		// Toast.makeText(context, message, Toast.LENGTH_LONG).show();
         View view = layoutInflater.inflate(R.layout.activity_toast, null);
         TextView textView = (TextView)view.findViewById(R.id.text);
         textView.setText(message);
         
         Toast toast = new Toast(context);
         toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
         toast.setDuration(Toast.LENGTH_LONG);
         toast.setView(view);
         toast.show();
	}
}


低耦合调用:

public class MainActivity extends Activity {

	private Button button;
	private Button button2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.button1);
		button2 = (Button) this.findViewById(R.id.button2);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				CustomDialog dialog = new CustomDialog(MainActivity.this);
				dialog.createDialog("确定", "提示", "您确定要删除吗?", new CallBack() {
					@Override
					public void isConfirm(boolean flag) {
						// TODO Auto-generated method stub
						System.out.println("----->>" + flag);
						if (flag) {
							//dosomething.....判断执行业务逻辑
						}
					}
				});
			}
		});
		button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
                 CustomDialog dialog = new CustomDialog(MainActivity.this);
                // LayoutInflater layoutInflater = getLayoutInflater();
                 dialog.createToasts("网络有有异常!!",getLayoutInflater());
                 
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


布局: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/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="dialog的提示" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="52dp"
        android:text="toasts提示" />

</RelativeLayout>

布局:activity_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DAAA"
    android:orientation="horizontal"
    android:padding="8dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:src="@drawable/alarm"
         />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />

</LinearLayout>





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值