所以我正在尝试创建一个设置屏幕,它似乎工作,直到我意识到首选项不会对点击做出反应。我尝试使用onPreferenceStartFragment,但是我遇到了一个奇怪的错误
boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref)
这是onPreferenceStartFragment:
public interface OnPreferenceStartFragment {
boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref);
//Syntax error on token ";", { expected after this token
if(pref == password) {
LayoutInflater inflater = LayoutInflater.from(context);
final View text = inflater.inflate(R.layout.changepassword, null);
final EditText currentPassword = (EditText)text.findViewById(R.id.currentPassword);
final EditText newPassword = (EditText)text.findViewById(R.id.newPassword);
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Change Your Password");
alert.setView(text);
alert.setPositiveButton("Change", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String stringData = currentPassword.getText().toString().trim();
String stringNew = newPassword.getText().toString().trim();
dataReturned = myFolder.getString("passwordKey", "");
if(dataReturned.equals(stringData)) {
String newData = newPassword.getText().toString().trim();
SharedPreferences.Editor editor = myFolder.edit();
editor.putString("passwordKey", newData);
editor.commit();
dataReturned = myFolder.getString("passwordKey", "couldn't load data");
Toast.makeText(context, "Password changed", Toast.LENGTH_SHORT).show();
currentPassword.setText("");
newPassword.setText("");
}
else {
Toast.makeText(context, "Incorrect Password", Toast.LENGTH_LONG).show();
currentPassword.setText("");
newPassword.setText("");
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alert.show();
;
}
if(pref == notification) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.device_access_secure)
.setOngoing(true)
.setContentTitle("Obstruct")
.setContentText("Start Stealth Mode");
Intent resultIntent = new Intent();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
return true;
}
一切似乎都是正确的,但它让我感到沮丧,事实并非如此。任何想法?
答案 0 :(得分:0)
我不太熟悉Java接口,但我怀疑编译器期望你定义一个名为onPreferenceStartFragment的方法。当然,鉴于行
之后的下一个语句,您的代码似乎也表明了这一点boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref);
是一个if语句。
您可能希望通过定义onPreferenceStartFragment方法及其在界面中的正文来查看括号以及您要实现的目标。事实上,在快速搜索之后,我甚至不确定你是否可以在接口中定义方法。
编辑:为了正确起见,我将补充说,接口中定义的默认方法体是Java 8中的可用功能。但Android不支持Java 8,因此不支持接口中定义的方法体。见What is the "default" implementation of method defined in an Interface?
答案 1 :(得分:0)
boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref);
is a method, and the code that you have below it is what is executed upon calling this method. There should be not semicolon at the end of the line, and it should be an opening bracket that starts the method.
It should be:
boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref){
if(pref == password) {
... <the rest of your code here>
...
return true;
}