Android自定义对话框

2014-11-24 11:50:13 · 作者: · 浏览: 2

代码实现如下:
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" >
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


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



custom_dialog.xml文件
< xml version="1.0" encoding="utf-8" >
http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入名字:" >

android:layout_height="wrap_content"
android:id="@+id/file_name"
android:lines="1">