Android之Dialog自定义对话框
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.demo_dialog.MainActivity" android:orientation="horizontal" > <Button android:id="@+id/main_custiom_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DIY"/> <Button android:id="@+id/main_custiom_window_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DIYWindow"/> </LinearLayout>
MainActivity.java
1 import android.app.Activity; 2 import android.app.AlertDialog; 3 import android.app.DatePickerDialog; 4 import android.app.DatePickerDialog.OnDateSetListener; 5 import android.app.ProgressDialog; 6 import android.app.TimePickerDialog; 7 import android.app.TimePickerDialog.OnTimeSetListener; 8 import android.content.DialogInterface; 9 import android.content.DialogInterface.OnMultiChoiceClickListener; 10 import android.os.Bundle; 11 import android.util.Log; 12 import android.view.View; 13 import android.view.Window; 14 import android.widget.Button; 15 import android.widget.DatePicker; 16 import android.widget.EditText; 17 import android.widget.TimePicker; 18 19 20 public class MainActivity extends Activity { 21 22 private Button btCustom; 23 private Button btWindow; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 btCustom = (Button)findViewById(R.id.main_custiom_dialog); 30 btCustom.setOnClickListener(clickListener); 31 32 btWindow = (Button)findViewById(R.id.main_custiom_window_dialog); 33 btWindow.setOnClickListener(clickListener); 34 } 35 36 private View.OnClickListener clickListener = new View.OnClickListener() { 37 38 @Override 39 public void onClick(View v) { 40 switch (v.getId()) { 41 case R.id.main_custiom_dialog: 42 showCusTomDialog(); 43 break; 44 case R.id.main_custiom_window_dialog: 45 showCusTomWindowDialog(); 46 break; 47 } 48 } 49 }; 50 51 private void showCusTomDialog(){ 52 View v = getLayoutInflater().inflate(R.layout.layout_dialog, null); 53 54 EditText et1 = (EditText)v.findViewById(R.id.dialog_et1); 55 Button bt = (Button)v.findViewById(R.id.dialog_ok); 56 57 AlertDialog dialog = new AlertDialog.Builder(this).setView(v).show(); 58 59 // 设置dialog不可触摸边缘取消(消失) 60 dialog.setCanceledOnTouchOutside(false); 61 // 设置对话框能否取消 62 dialog.setCancelable(false); 63 // 取消对话框 Cancelable(false)时cancel()无效 64 dialog.cancel(); 65 // 是对话框消失 66 dialog.dismiss(); 67 68 69 // EditText et1 = (EditText)dialog.findViewById(R.id.dialog_et1); 70 } 71 72 private void showCusTomWindowDialog(){ 73 AlertDialog dialog = new AlertDialog.Builder(this).create(); 74 // 获得dialog所在的window 75 Window w = dialog.getWindow(); 76 dialog.show(); 77 // 设置窗体的内容 必须在show()方法之后使用 78 w.setContentView(R.layout.layout_dialog); 79 80 EditText et1 = (EditText)dialog.findViewById(R.id.dialog_et1); 81 // EditText et1 = (EditText)w.findViewById(R.id.dialog_et1); 82 } 83 84 }