弹出一个消息框,在android中可以这样实现
新建一个AlertDialog,并用Builder方法形成了一个对象链,通过一系列的设置方法,构造出我们需要的对话框,然后调用show方法显示出来
new AlertDialog.Builder(self)
.setTitle("标题")
.setMessage("简单消息框")
.setPositiveButton("确定", null)
.show();
带确认和取消按钮的对话框
new AlertDialog.Builder(self)
.setTitle("确认")
.setMessage("确定吗?")
.setPositiveButton("是", null)
.setNegativeButton("否", null)
.show();
null是点击事件
可以输入文本的对话框
new AlertDialog.Builder(self)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(new EditText(self))
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
单选框与多选框,也是非常有用的两种对话框
new AlertDialog.Builder(self)
.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
)
.setNegativeButton("取消", null)
.show();
new AlertDialog.Builder(self)
.setTitle("多选框")
.setMultiChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, null, null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
列表对话框
new AlertDialog.Builder(self)
.setTitle("列表框")
.setItems(new String[] {"列表项1","列表项2","列表项3"}, null)
.setNegativeButton("确定", null)
.show();
在对话框中显示图片
ImageView img = new ImageView(self);
img.setImageResource(R.drawable.icon);
new AlertDialog.Builder(self)
.setTitle("图片框")
.setView(img)
.setPositiveButton("确定", null)
.show();
对话框
//加载布局
View view = LayoutInflater.from(mActivity).inflate(ResourcesUtil.
getLayout("pd_sdk_dialog_user_guest_login"), null);
dialog = new AlertDialog.Builder(mActivity).create();
// 确定按钮
ok = ((Button) view.findViewById(ResourcesUtil.getId("pd_sdk_guest_login_ok")));
//返回按钮
back = ((ImageView) view.findViewById(ResourcesUtil.getId("pd_sdk_login_guest_back")));
//确认按钮
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//返回按钮
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
/*获得对话框的属性*/
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.gravity = Gravity.CENTER;
//设置对话框的宽高
params.width = getResources().getDimensionPixelSize(ResourcesUtil.getDimen("pd_sdk_dialog_layout_main_w"));
params.height = getResources().getDimensionPixelSize(ResourcesUtil.getDimen("pd_sdk_dialog_layout_main_h"));
dialog.getWindow().setAttributes(params);
//设置布局
dialog.getWindow().setContentView(view);
lnearLayout
style="@style/pd_sdk_dialog_root_layout_style"
android:layout_centerInParent="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/pd_sdk_login_guest_back"
style="@style/pd_sdk_dialog_back_btn_style" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/pd_sdk_fast_login_titles"
android:textColor="@color/pd_sdk_color_B21"
android:textSize="@dimen/pd_sdk_dialog_login_word" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/pd_sdk_dialog_line"
android:orientation="horizontal">
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pd_sdk_fast_login_1"
android:textColor="@color/pd_sdk_color_B11" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pd_sdk_fast_login_2"
android:textColor="@color/pd_sdk_color_B11" />
<Button
android:id="@+id/pd_sdk_guest_login_ok"
android:layout_width="match_parent"
android:layout_height="@dimen/pd_sdk_dialog_common_btn_h"
android:background="@drawable/pd_sdk_btn_login_color"
android:text="@string/pd_sdk_fast_login_button"
android:textColor="@color/pd_sdk_color_white"
android:textSize="@dimen/pd_sdk_dialog_login_word" />
</LinearLayout>