package com.easyway.android.diaglog; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; /** * Android获取dialog自定义布局中的控件 中特定的对象比较特殊必须通过 * //获取布局相关的对象 * LayoutInflater inflater=LayoutInflater.from(this); * //获取相关的布局 * View layout = inflater.inflate(R.layout.send_email_dialog, (ViewGroup) findViewById(R.id.send_email_dialog_ll)) * final EditText sendEmailCon_ET = (EditText) layout.findViewById(R.id.send_email_dialog_et); * * * @author longgangbai * */ public class CustomDiaglogActivity extends Activity { private final int CUSTOM_DIALOG = 1; private Button diagbtn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置当前的布局 setContentView(R.layout.main); //获取按钮 diagbtn=(Button)findViewById(R.id.btndiaglog); //设置按钮的事件 View.OnClickListener dateBtnListener = new BtnOnClickListener(CUSTOM_DIALOG); diagbtn.setOnClickListener(dateBtnListener); } /** * 重写Activity的方法实现相关的不同 * * 按钮的实现不同对话框 * * * 想要获取自定义布局中的控件必须调用你所定义的View的findViewById方法,而不能像获得其他控件一样直接调用findViewById方法。。。 */ @Override protected Dialog onCreateDialog(int id) { //自定义对话框 AlertDialog.Builder builder= new Builder(CustomDiaglogActivity.this); //获取布局相关的对象 LayoutInflater inflater=LayoutInflater.from(this); //获取相关的布局 View layout = inflater.inflate(R.layout.send_email_dialog, (ViewGroup) findViewById(R.id.send_email_dialog_ll)); //设置相关的视图 builder.setView(layout); //设置标题 builder.setTitle("反馈意见"); //确定按钮的操作 builder.setPositiveButton("确认",new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { //销毁对话框 dialog.dismiss(); CustomDiaglogActivity.this.finish(); } }); //取消按钮的操作 builder.setNegativeButton("取消",new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); return builder.create(); } /** * 成员内部类,此处为提高可重用性,也可以换成匿名内部类 */ private class BtnOnClickListener implements View.OnClickListener { private int dialogId = 0; //默认为0则不显示对话框 public BtnOnClickListener(int dialogId) { this.dialogId = dialogId; } @Override public void onClick(View view) { showDialog(dialogId); } } }
send_mail_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/send_email_dialog_ll" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_height="wrap_content" android:text="To : longgangbai@sina.com" android:layout_gravity="center" android:layout_marginTop="4dp" /> <EditText android:id="@+id/send_email_dialog_et" android:layout_height="wrap_content" android:layout_width="fill_parent" android:inputType="textMultiLine" android:lines="4" android:hint="@string/message" android:scrollbarAlwaysDrawVerticalTrack="true" android:layout_marginTop="9dp"> <requestFocus></requestFocus> </EditText> </LinearLayout>