Android自定义对话框

在android中有自带的对话框,为了美观,很多开发者会使用自定义对话框,如下图:

Android自定义对话框

点击“弹出自定义对话框按钮后”显示如图效果。

首先要自己定义一个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="match_parent"
android:background="@drawable/background"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" > <ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/airsign" /> <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3"
android:gravity="center"
android:text="请输入密码"
android:textColor="#FFFFFFFF"
android:textSize="32px"
android:textStyle="bold" />
</LinearLayout> <EditText
android:id="@+id/et_pass"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/tv_background"
android:paddingLeft="10dp"/> <LinearLayout
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="40dp"
style="?android:attr/buttonStyleSmall"
android:layout_weight="1"
android:background="@drawable/background"
android:textColor="#FFFFFFFF"
android:text="确定"/>
<Button
android:id="@+id/btn_delete"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="40dp"
style="?android:attr/buttonStyleSmall"
android:layout_weight="1"
android:textColor="#FFFFFFFF"
android:background="@drawable/background"
android:text="取消"/>
</LinearLayout>
</LinearLayout>

mainActivity中:

public class MainActivity extends Activity {
private Context context = MainActivity.this; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void clickView(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 创建对话框
AlertDialog dialog = builder.create();
// 没有下面这句代码会导致自定义对话框还存在原有的背景
builder.setView(View.inflate(this, R.layout.auto_dialog, null));
// 弹出对话框
dialog.show();
// 以下两行代码是对话框的EditText点击后不能显示输入法的
dialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
// *** 主要就是在这里实现这种效果的.
// 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
Window window = dialog.getWindow();
window.setContentView(R.layout.auto_dialog);
EditText et_pass = (EditText) window.findViewById(R.id.et_pass);
final Button btn_ok = (Button) window.findViewById(R.id.btn_ok);
final Button btn_delete = (Button) window.findViewById(R.id.btn_delete);
// 为两个button按钮添加点击的监听事件
btn_ok.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, btn_ok.getText(), 0).show();
}
});
btn_delete.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, btn_delete.getText(), 0).show();
}
});
}
}
上一篇:css悬浮提示框


下一篇:Android 常见对话框的简单使用(提示信息对话框、单选多选对话框、自定义对话框)