Android自定义对话框


本文的Android自定义对话框效果图如下:

Android自定义对话框

代码实现如下:
CustomDialogActivity.java文件
package wzhnsc.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class CustomDialogActivity extends Activity {
  
  
    private Button custom = null;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        custom = (Button)this.findViewById(R.id.custom);
        custom.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v) {
               // TODO Auto-generated method stub
                showCustomDia();
            }
         });
    }
  
  
    private void showCustomDia()
    {
        AlertDialog.Builder customDia=new AlertDialog.Builder(CustomDialogActivity.this);
        final View viewDia=LayoutInflater.from(CustomDialogActivity.this).inflate(R.layout.custom_dialog, null);
        customDia.setTitle("新建文件夹");
        customDia.setView(viewDia);
        customDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
          
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                EditText diaInput=(EditText) viewDia.findViewById(R.id.file_name);
              
              
            }
        });
        customDia.create().show();
    }
  
}

main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/custom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义对话框" />

</LinearLayout>

custom_dialog.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:orientation="vertical" >
  

    <TextView     
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入名字:" >
    </TextView>
    <EditText android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/file_name"
              android:lines="1">
    </EditText>  
</LinearLayout>

相关内容