系统默认的弹框太丑陋了,可以根据个人的喜好自定义弹框,弹框页面布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/editpassword"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:background="#bfbfbf"
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_height="35dp"
android:ems="10" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp">
<Button
android:layout_marginLeft="20dp"
android:id="@+id/ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/ok"
android:background="#696969"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:id="@+id/cancle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#696969"
android:text="@string/cancle"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
按钮点击事件:
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showDeviceName();
}
});
public void showDeviceName(){
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=getActivity().getLayoutInflater();
final View pswLayout=inflater.inflate(R.layout.edit_password, (ViewGroup)getActivity().findViewById(R.id.editpassword));
Button btn_ok=(Button) pswLayout.findViewById(R.id.ok);
Button btn_cancle=(Button) pswLayout.findViewById(R.id.cancle);
btn_ok.setOnClickListener(new OnClickListener() { //弹框里"确定"按钮的点击事件
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText etPassWord=(EditText) pswLayout.findViewById(R.id.pwd);
String name=etPassWord.getText().toString().trim();//获取输入的用户名
if(!name.equals("")){//如果输入的名字不为空
btName.setText(name);
setupinterface.setDeviceName(name);//设置系统的名字为输入的名字(方法略)
showPasswordDialog.dismiss();
}else{
showPasswordDialog.dismiss();//如果是空,则返回
}
}
});
btn_cancle.setOnClickListener(new OnClickListener() {//弹框中"取消"按钮的点击事件:
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showPasswordDialog.dismiss();
}
});
builder.setTitle(R.string.setName);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setView(pswLayout);
showPasswordDialog = builder.create();
showPasswordDialog.show();
}
注意:在判断用户有没有输入的时候,name !=null && !name.equals("")判断条件。为null的意思是存不存在,.equals("")的意思是内容为空,但是它存在。