先定义一个layout作为弹出框的content
<?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:orientation="vertical" >
<EditText
android:id="@+id/edUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="username" />
</LinearLayout>
再通过AlertDialog来实现
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("游戏结束,请英雄留下您的称谓");
// 通过LayoutInflater来加载一个xml的布局文件作为一个View对象
View view = LayoutInflater.from(getContext()).inflate(
R.layout.dialog, null);
// 设置我们自己定义的布局文件作为弹出框的Content
builder.setView(view);
final EditText username = (EditText) view
.findViewById(R.id.edUsername);
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//确定操作的内容
}
});
builder.setNegativeButton("算了",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "告辞!",
Toast.LENGTH_SHORT).show();
}
});
builder.show();
最后的结果