2013-03-05 134 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

相关问题