Android 自定义格式的对话框

 

 

1、取消系统默认对话框格式

 <style name="dialog" parent="@android:style/Theme.Dialog">  
         <item name="android:windowFrame">@null</item>  
         <item name="android:windowIsFloating">true</item>  
         <item name="android:windowIsTranslucent">false</item>  
         <item name="android:windowNoTitle">true</item>  
         <item name="android:background">@android:color/transparent</item>
         <item name="android:windowBackground">@android:color/transparent</item>
         <item name="android:backgroundDimEnabled">false</item>  
 </style>   

 

<item name="android:backgroundDimEnabled">true</item>是设置对话框背后的半透明效果  默认为显示

 

2、设置布局 dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/login_dialog_bg"
    android:padding="26dip"
    android:orientation="vertical">
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="用户名:"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textColor="@color/black"/>
        <EditText 
            android:id="@+id/EtUserName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="18sp"
            android:textStyle="bold"
            android:background="@drawable/login_dialog_edittext_bg"
            android:textColor="@color/black"/>
    </LinearLayout>
 
     <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="密    码:"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textColor="@color/black"/>
        <EditText 
            android:id="@+id/EtPassword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="18sp"
            android:textStyle="bold"
            android:paddingLeft="10dip"
            android:background="@drawable/login_dialog_edittext_bg"
            android:textColor="@color/black"/>
    </LinearLayout>
    
     <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip">
        <Button 
            android:id="@+id/BtnDialogCancle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="取消"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_marginRight="25dip"
            android:textColor="@color/white"
            android:background="@drawable/btn_login_dialog"/>
        <Button 
            android:id="@+id/BtnDialogLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="25dip"
            android:gravity="center"
            android:textSize="15sp"
            android:textStyle="bold"
            android:paddingLeft="10dip"
            android:text="登录"
            android:textColor="@color/white"
            android:background="@drawable/btn_login_dialog"/>
    </LinearLayout>
   
</LinearLayout>

 

3、生成对话框

    //生成一个对话框,在style里取消对话框的任何默认格式
        loginDialog =  new Dialog(IdentityUnLoginActivity.this, R.style.dialog);
        loginDialog.setContentView(R.layout.login_dialog);
    
EtUserName
=(EditText)loginDialog.findViewById(R.id.EtUserName);; EtPassword = (EditText)loginDialog.findViewById(R.id.EtPassword); BtnDialogCancle = (Button)loginDialog.findViewById(R.id.BtnDialogCancle); BtnDialogLogin = (Button)loginDialog.findViewById(R.id.BtnDialogLogin);
loginDialog.show();
    
        BtnDialogCancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                loginDialog.dismiss();
            }
        });
        
        BtnDialogLogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                
                String username = EtUserName.getText().toString();
                String userpass = EtPassword.getText().toString();
                if(null == username || username.equals(""))
                {
                    Toast.makeText(IdentityUnLoginActivity.this, "请输入用户名",
                            Toast.LENGTH_SHORT).show();
                }else if(null == userpass || userpass.equals("")){
                    Toast.makeText(IdentityUnLoginActivity.this, "请输入密码",
                            Toast.LENGTH_SHORT).show();
                }else{
                    loginDialog.dismiss();
                    if(BtnDialogLogin.getText().equals("登录")){
                        //HttpUtils.userLogin();
                        Intent intent = new Intent();
                        intent.setClass(IdentityUnLoginActivity.this, IdentityActivity.class);
                        startActivity(intent);
                       }else if(BtnDialogLogin.getText().equals("注册")){
                        HttpUtils.userRegister(username,userpass);
                    }
                }
            }
        });

 

 
posted @ 2013-07-04 21:06  马小豆包  阅读(346)  评论(0编辑  收藏  举报