Android 关于Dialog弹出框

直接上效果图:

实现步骤:

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"
    android:background="@drawable/skin_background"
 >

    <Button
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="退出系统"
        android:textSize="20sp" />

</RelativeLayout>

2.弹出层样式actionsheet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/transparent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/actionsheet_top_normal"
        android:gravity="center"
        android:text="@string/title"
        android:textColor="#8F8F8F"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/actionsheet_bottom_selector"
        android:gravity="center"
        android:text="@string/content"
        android:textColor="#FD4A2E"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/actionsheet_single_selector"
        android:gravity="center"
        android:text="@string/cancel"
        android:textColor="#037BFF"
        android:textSize="16sp" />

</LinearLayout>

3.ActionSheet类

public class ActionSheet {

	public interface OnActionSheetSelected {
		void onClick(int whichButton);
	}

	private ActionSheet() {
	}

	public static Dialog showSheet(Context context, final OnActionSheetSelected actionSheetSelected,
			OnCancelListener cancelListener) {
		final Dialog dlg = new Dialog(context, R.style.ActionSheet);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.actionsheet, null);
		final int cFullFillWidth = 10000;
		layout.setMinimumWidth(cFullFillWidth);

		TextView mContent = (TextView) layout.findViewById(R.id.content);
		TextView mCancel = (TextView) layout.findViewById(R.id.cancel);

		mContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actionSheetSelected.onClick(0);
				dlg.dismiss();
			}
		});

		mCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actionSheetSelected.onClick(1);
				dlg.dismiss();
			}
		});

		Window w = dlg.getWindow();
		WindowManager.LayoutParams lp = w.getAttributes();
		lp.x = 0;
		final int cMakeBottom = 0;
		lp.y = cMakeBottom;
		lp.gravity = Gravity.CENTER;
		dlg.onWindowAttributesChanged(lp);
		dlg.setCanceledOnTouchOutside(false);
		if (cancelListener != null)
			dlg.setOnCancelListener(cancelListener);

		dlg.setContentView(layout);
		dlg.show();

		return dlg;
	}

}

4.MainActivity

public class MainActivity extends Activity implements OnActionSheetSelected, OnCancelListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.show).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ActionSheet.showSheet(MainActivity.this, MainActivity.this, MainActivity.this);
			}
		});
	}

	@Override
	public void onClick(int whichButton) {
		// TODO Auto-generated method stub
		switch (whichButton) {
			case 0:
				showToast("确定");
				break;

			case 1:
				showToast("取消");
				break;

			default:
				break;
		}

	}

	@Override
	public void onCancel(DialogInterface dialog) {
		// TODO Auto-generated method stub
		showToast("取消");
	}

	private void showToast(CharSequence charSequence) {
		Toast.makeText(this, charSequence, Toast.LENGTH_SHORT).show();
	}
}

 源码下载地址

时间: 2024-08-07 10:23:33

Android 关于Dialog弹出框的相关文章

android service Dialog 弹出框

android service Dialog 弹出框 相信大家第一次在Service中实现 AlertDialog 弹出框时,都会遇到应用闪退然后报出这个异常: Caused by: android.view.WindowManager$BadTokenException: 下面说下为什么出现这个异常,原因很简单,是由于 AlertDialog 的显示是依赖于一个确定的Activity类,所以要想在 Service 中实现弹出来,需要做如下配置: 1.安装常规写好 AlertDialog 功能块

Android自定义控件:可复用的Dialog弹出框

最近帮工作室改一个项目,需求是制作许多单选.多选的Dialog弹出框,我感觉有许多代码都是可重用的,就写了个可重用的Dialog类,废话不多说,先看图: 由于一些和谐的原因,实际效果肯定是比这个好看的,这里基本上都是原生属性修改--简单讲一下设计思路吧: 为什么选择自定义DIalog子类而不是AlertDialog子类(或者其他)? Dialog子类是诸如AlertDialog子类等的父类,其可自定义范围更广(因为被设计的子类属性.方法等更少,同时又具备必要的属性和方法),其次就是,有些子类的设

Android窗口为弹出框样式

1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fish.helloworld" android:versio

android 三种弹出框之一poprpWindow

poprpWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,poprpWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看看poprpWindow API 给出的解释是: 意思就是一个展示view的弹出窗体,这个弹出窗体将会浮动在当前activity的最上层, 它和AlertDialog的区别是:在android中弹出框有两种方式:AlertDialog和PopupWindow,它们的不同点在于:      1.Ale

dialog弹出框 点击周围空白处弹出层不自动消失

dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失 dialog弹出框 点击周围空白处弹出层不自动消失

使用easeui dialog弹出框中使用CKeditor多次加载后无法编辑问题

问题呈现:弹出框页面 <tr class="addtr"> <th>内容</th> <td> <!-- <textarea rows="15" cols="50" id="content" name="content" class="ckeditor">请输入.</textarea> --> <te

dialog弹出框,自定义里面的布局;

//代码里面的设置,点击触发弹出对话框: case R.id.re_ps: // 配送费 final Dialog dialog1 = new Dialog(this); View contentView1 = LayoutInflater.from(this).inflate( R.layout.activity_send_peisong, null); dialog1.setContentView(contentView1); dialog1.setTitle("配送费用"); d

Android 大约Dialog弹出窗口

直接效果图: 实现步骤: 1.主界面activity_main.xml非常easy,一个button <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layou

android开发学习 ------- 弹出框

这是一种方法,是我觉得简单易懂代码量较少的一种: /* 创建AlertDialog对象并显示 */ final AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create(); alertDialog.show(); /* 添加对话框自定义布局 */ alertDialog.setContentView(R.layout.dialog_login); /* 获取对话框窗口 */ Window windo