簡單的作法可以參考 [Android] AlertDialog-嵌入客製化表單
在 AlertDialog 中加入 EditText,不過上面沒有取得 EditText 內容的方法
下面是我的 code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
public class customizeDialog extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .addPreferencesFromResource(R.xml.app_settings); Preference prefBackup = this .findPreference( this .Pref_Backup); prefBackup.setOnPreferenceClickListener( this .onPrefClick); } //--------------------------------------------------------// //------------------------ events ------------------------// //--------------------------------------------------------// public OnPreferenceClickListener onPrefClick = new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference pref) { // TODO Auto-generated method stub if (pref.getKey().compareTo( "dialogWithEditText" ) == 0 ) showDialog(R.id.dialogEdit); return false ; } }; public Dialog onCreateDialog( int id) { Dialog dialog = null ; switch (id) { case R.id.dialogEdit: AlertDialog.Builder builder = new AlertDialog.Builder(AppSettings. this ); // 取得 View LayoutInflater inf = LayoutInflater.from(AppSettings. this ); View linear = inf.inflate(R.layout.dialog_with_edittext, null ); builder.setView(linear); // 設定輸入框 final EditText edit = (EditText) linear.findViewById(R.id.dialogEdit); builder.setPositiveButton( "確定" , new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(customizeDialog. this , "輸入" + edit.getText(), Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(customizeDialog. this , "按下取消按鈕" , Toast.LENGTH_SHORT).show(); } }); builder.setTitle( "對話框標題" ); builder.setMessage( "對話框文字" ); dialog = builder.create(); break ; } return dialog; } } |
這是用了 onCreateDialog( ) 跟 showDialog( ) 函式的顯示對話框的方式。
順道提一下,原本沒有用這個的時候一直出現奇怪的錯誤訊息 XD
像是 WindowsManager ..... 什麼的 Exception~
記一下原本一直出錯的程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
public class customizeDialog extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .addPreferencesFromResource(R.xml.app_settings); Preference prefBackup = this .findPreference( this .Pref_Backup); prefBackup.setOnPreferenceClickListener( this .onPrefClick); } //--------------------------------------------------------// //------------------------ events ------------------------// //--------------------------------------------------------// public OnPreferenceClickListener onPrefClick = new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference pref) { // TODO Auto-generated method stub if (pref.getKey().compareTo( "dialogWithEditText" ) == 0 ) { AlertDialog.Builder builder = new AlertDialog.Builder(AppSettings. this ); // 取得 View LayoutInflater inf = LayoutInflater.from(AppSettings. this ); View linear = inf.inflate(R.layout.dialog_with_edittext, null ); builder.setView(linear); // 設定輸入框 final EditText edit = (EditText) linear.findViewById(R.id.dialogEdit); builder.setPositiveButton( "確定" , new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(customizeDialog. this , "輸入" + edit.getText(), Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(customizeDialog. this , "按下取消按鈕" , Toast.LENGTH_SHORT).show(); } }); builder.setTitle( "對話框標題" ); builder.setMessage( "對話框文字" ); dialog = builder.create(); } } } } |
兩種寫法的差別就是上面的是把建立 Dialog 的語法寫在 onCreateDialog 裡
下面則是直接寫在 onPreferenceClick 裡(會出現錯誤)
沒有留言:
張貼留言