天天看点

AlertDialog Builder自定义样式

public void LoginDialog(final Context context){
  AlertDialog.Builder builder = new AlertDialog.Builder(context);
  LayoutInflater inflaterDl = LayoutInflater.from(this);
   LinearLayout layout = (LinearLayout)inflaterDl.inflate(R.layout.logindialog, null );

  final EditText userNameET = (EditText)layout.findViewById(R.id.username);
  final EditText passwordET = (EditText)layout.findViewById(R.id.password);
  
  builder.setTitle(“登录”).setPositiveButton(确定“”), new DialogInterface.OnClickListener() {
   
   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
   String  username = userNameET.getText().toString();
   String password = passwordET.getText().toString();
       }
  }).setNegativeButton("取消"), new DialogInterface.OnClickListener() {
   
   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    
   }
  });
  builder.setView(layout);
  AlertDialog ad = builder.create();
  ad.show();
 }                
logindialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
   <TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:text="@string/username" 
  android:layout_weight="0.05"/>
 <EditText android:id="@+id/username"   
  android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:drawable/editbox_background" 
        android:layout_weight="0.04"/>

 <TextView android:layout_width="fill_parent"
  android:text="@string/password" 
  android:layout_height="wrap_content" 
  android:layout_weight="0.04"/>
 <EditText android:id="@+id/password"   
  android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textPassword"
        android:background="@android:drawable/editbox_background" 
        android:layout_weight="0.04"/>
</LinearLayout>
           

版权声明:本文为CSDN博主「weixin_34245082」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_34245082/article/details/91902734