我在Android Studio上做了一些非常基础的编程。 我在屏幕上收到此错误:
无法解决方法' SetPositiveButton'
我缺少import
吗?
这是我的完整代码:
package com.kontrol.app;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
public class SS1_Senha extends DialogPreference implements DialogInterface.OnClickListener{
public SS1_Senha(Context context, AttributeSet attrs) {
super(context, attrs);
setPersistent(false);
setDialogLayoutResource(R.layout.ss1_senha);
SetPositiveButtonText("OK");
SetPositiveButtonText("Cancelar")
setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Action after OK
}
});
}
}
答案 0 :(得分:2)
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("some text");
alertDialogBuilder.setCancelable(true);
// set dialog message
alertDialogBuilder
.setMessage("some text")
.setCancelable(true)
.setPositiveButton( "Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
try {
//so some work
} catch (Exception e) {
//Exception
}
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//do something if you need
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
答案 1 :(得分:1)
您可以检查this答案并直接在XML中执行DialogPreference:
<com.kontrol.app.SS1_Senha
android:key="@string/the_key"
android:dialogIcon="@android:drawable/ic_dialog_alert"
android:title="@string/your_title"
android:summary="@string/your_summary"
android:dialogMessage="@string/your_text"
android:positiveButtonText="Ok"
android:negativeButtonText="Cancelar"/>
答案 2 :(得分:0)
确保您看到此方法
setPositiveButton(CharSequence text,DialogInterface.OnClickListener listener)
当您开始输入.setPos ...时,请选择以上选项。