我有一个使用共享首选项的 alertDialog,因此该对话框只能显示一次。你们中的一些人可能知道它导致我的活动表现得很有趣,我的意思是没有响应我的按钮的 onClick。有人告诉我将我的活动的下半部分从 之后开始的共享偏好声明中alert.show();
移出我遇到的问题是我不知道将我的活动的下半部分移到哪里。不知道该不该搬if
if (!initialDialogDisplayed) {
final EditText et = (EditText) findViewById(R.id.editText1);
Button getAnswer = (Button) findViewById(R.id.button1);
getAnswer.setOnClickListener(new OnClickListener() {
以上共享偏好或什么。所以我希望有人能真正帮助我解决这个问题。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
if (!initialDialogDisplayed) {
Editor editor = preferences.edit();
editor.putBoolean("InitialDialog", true);
editor.commit();
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Alert");
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setMessage("Dialog");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
final EditText et = (EditText) findViewById(R.id.editText1);
Button getAnswer = (Button) findViewById(R.id.button1);
getAnswer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (et.getText().toString().length()==0) {
Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();
}else{
EditText et = (EditText) findViewById(R.id.editText1);
String searchTerm = et.getText().toString().trim();
Intent in = new Intent(MainActivity.this, ListView.class);
in.putExtra("TAG_SEARCH", searchTerm);
startActivity(in);
}
}
});
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}}