私はいくつかのアクティビティでアプリケーションを作成しました。すべてのアクティビティで、私はonOptionsItemSelectedメソッドとonCreateOptionsMenuメソッドに上書きします。私のonOptionsItemSelectedメソッドは静かなので、すべてのアクティビティでそのコードセグメントを繰り返す必要があります。どのようにしてすべてのアクティビティに適用できる単一のメソッドを作成できますか?ここに私のonOptionsItemSelectedメソッドとonCreateOptionsMenuメソッドがあります。単一のメソッドからオプションの操作を操作する方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem loginItem = menu.findItem(R.id.action_login);
User u = sharedPreference.getUser(BaseActivity.this);
if (u.isLogged()) {
loginItem.setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_login) {
if (loggedUser.isLogged())//
{
builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(menuDialogMessage)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
} else {
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
return true;
}
} else if (id == R.id.action_settings) {
builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(menuDialogMessage)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), Settings.class));
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
} else if (id == R.id.action_change_pw) {
builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(menuDialogMessage)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), ChangePassword.class));
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
} else if (id == R.id.action_logout) {
builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(menuDialogMessage)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sharedPreference.removeUser(BaseActivity.this);
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
} else if (id == R.id.action_exit) {
builder = new AlertDialog.Builder(BaseActivity.this);
builder.setMessage(exit)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
Globals.mContext.finish();
System.exit(1);
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
あなたは別のクラスを作成し、 'id'や' messageContent'や 'messageTitle'などのパラメータを' context'とともに渡してそこからダイアログを呼び出すことができます。そうでなければ涼しいと分かっていれば、私はあなたのためにいくつかのスニペットを書くコメントを追加します。 – Ashwani