2013-03-05 137 views
1

在我的Android應用程序中,我顯示包含edittext的對話框。這個對話框是使用PreferenceCategory。我xml文件看起來像按下對話框(確定,取消)按鈕獲取事件(Android)

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

    <PreferenceCategory android:title="@string/security_setting_edittext_hint" > 
     <EditTextPreference 
     android:dialogTitle="@string/security_setting_button" 
     android:key="set_password_preference" 
     android:summary="@string/set_password_summary" 
     android:title="@string/security_setting_button" 
     android:inputType="number" 
     android:icon="@drawable/lock" 
     /> 
    </PreferenceCategory> 

</PreferenceScreen> 

我的Java文件看起來像

public class Settings extends PreferenceActivity { 

    Dialog setPasswordDialog; 
    EditText setPassword; 
    EditTextPreference editPreference; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setTitle("Settings"); 
     addPreferencesFromResource(R.xml.preference_authentication); 
     editPreference=(EditTextPreference) findPreference("set_password_preference"); 

} 

有在顯示dialog沒有問題顯示但現在我想壽獲取事件時確定和取消按下對話框中的按鈕來執行某些操作。 請爲我提供解決方案。

+0

當你想使用的:

public class MyEditTextPreference extends EditTextPreference { public MyEditTextPreference(Context context) { super(context); } public MyEditTextPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyEditTextPreference(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: // Put your logic here for Ok button press break; case DialogInterface.BUTTON_NEGATIVE: // Put your logic here for Cancel button press break; } super.onClick(dialog, which); } } 

然後在XML文件中按如下方式使用它對話框使用PreferenceCategory顯示,您將需要創建一個自定義的EditTextPreference,如@appsroxcom的答案中所示。 – appsroxcom 2013-03-06 17:34:05

回答

1
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
    switch (which){ 
    case DialogInterface.BUTTON_POSITIVE: 
     //Yes button clicked 
     break; 

    case DialogInterface.BUTTON_NEGATIVE: 
     //No button clicked 
     break; 
    } 
    } 


}; 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) 
    .setNegativeButton("No", dialogClickListener).show(); 
3

如果我正確地得到你的問題,你要處理的「確定」和「取消」事件,然後執行基於響應一些行動。

// This is using code: 
AlertDialog.Builder alert = new AlertDialog.Builder(this); 
alert.setTitle("TITLE HERE"); 
alert.setMessage("MESSAGE"); 

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
    //Do something here where "ok" clicked 
    } 
}); 
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
    //So sth here when "cancel" clicked. 
    } 
}); 
alert.show(); 
+0

感謝您的回覆。如果我對這個答案的理解是正確的,那麼我不想創建新的對話框。我想使用PreferenceCategory – void 2013-03-05 14:37:07

3

您將需要創建自定義編輯文本首選項,如下所示。

<com.package.MyEditTextPreference 
android:dialogTitle="@string/security_setting_button" 
android:key="set_password_preference" 
android:summary="@string/set_password_summary" 
android:title="@string/security_setting_button" 
android:inputType="number" 
android:icon="@drawable/lock" 
/> 

其中com.package應該由實際的包在你的項目所取代,你創建MyEditTextPreference

+0

顯示的對話框,這個例子真的很牛逼,但如果我們不想解僱對話框,如果edittext爲null ... 。 – 2013-08-21 06:09:59

相關問題