如何继续收听onClick?

时间:2016-08-10 14:36:58

标签: android button android-alertdialog

我目前正在尝试创建一个“切换”按钮。一种按钮,如果某个布尔值为' true',那么它会弹出一个AlertDialog,而如果为false,它就不会弹出对话框。

然而,问题是目前它只是在活动开始时改变按钮的行为,而不是在实际改变布尔值时。

我认为这是因为我已将该方法放在onStart中,但我不太确定将代码放在何处。

如何继续收听onClick,以便用户在更改设置后立即更改按钮?

以下是我目前的代码:

public void onStart() {
        super.onStart();
        final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        final SharedPreferences.Editor editor = settings.edit();
        final Button StopButton = (Button) findViewById(R.id.StopButton);
        boolean RequestingLU = settings.getBoolean("RequestingLU", true);
        if (RequestingLU) {
            StopButton.setText(R.string.Stop_Button);
            StopButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this);
                    StopDialog.setTitle(R.string.Stop_Title);
                    StopDialog.setMessage(R.string.Stop_Message);
                    StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            editor.putBoolean("RequestingLU", false);
                            editor.apply();
                            Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    });
                    StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //Closes box
                            dialog.dismiss();
                        }
                    });
                    StopDialog.create().show();
                }
            });
        }
        else {
            StopButton.setText(R.string.Start_Button);
            StopButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editor.putBoolean("RequestingLU", true);
                    editor.apply();
                    Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

3 个答案:

答案 0 :(得分:1)

您可以尝试以下方式:

    if (RequestingLU) {
        StopButton.setText(R.string.Stop_Button);
    } else {
        StopButton.setText(R.string.Start_Button); 
    }

    StopButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String startText = getResources().getString(R.string.Start_Button);
            String stopText = getResources().getString(R.string.Stop_Button);

            String buttonText = StopButton.getText().toString();

            if(stopText.equals(buttonText)){
                AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this);
                StopDialog.setTitle(R.string.Stop_Title);
                StopDialog.setMessage(R.string.Stop_Message);
                StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        editor.putBoolean("RequestingLU", false);
                        editor.apply();
                        Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                        StopButton.setText(R.string.Start_Button);
                    }
                });
                StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Closes box
                        dialog.dismiss();
                    }
                });
                StopDialog.create().show();
            } else if(startText.equals(buttonText)){
                editor.putBoolean("RequestingLU", true);
                editor.apply();
                Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show();
                StopButton.setText(R.string.Stop_Button);
            }
        }
    });

希望它会对你有所帮助。

答案 1 :(得分:0)

我认为正确的方法是在onCreate()内声明它。

在仔细查看代码后,我无法看到您将RequestingLU布尔值声明为true的位置。

您需要声明如下:

StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int id) {

    editor.putBoolean("RequestingLU", true);
    editor.apply();

    dialog.dismiss();
                            }
                        });

StopDialog.setNeutralButton(R.string.Negative_Button, new    DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {

     editor.putBoolean("RequestingLU", false);
     editor.apply();
     //Closes box
     dialog.dismiss();
                            }
                        });

答案 2 :(得分:-1)

好的,我找到了解决方案。

我只是将整个代码块移动到onClickListener中。 也就是说,将布尔的RequestingLU检查放在onClickListener中,而不是相反。

完整代码:

final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        final SharedPreferences.Editor editor = settings.edit();
        final Button StopButton = (Button) findViewById(R.id.StopButton);
        StopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                boolean RequestingLU = settings.getBoolean("RequestingLU", true);
                if (RequestingLU) {
                    StopButton.setText(R.string.Start_Button);
                    AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this);
                    StopDialog.setTitle(R.string.Stop_Title);
                    StopDialog.setMessage(R.string.Stop_Message);
                    StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            editor.putBoolean("RequestingLU", false);
                            editor.apply();
                            Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    });
                    StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //Closes box
                            dialog.dismiss();
                        }
                    });
                    StopDialog.create().show();
                }
                else {
                    StopButton.setText(R.string.Stop_Button);
                    editor.putBoolean("RequestingLU", true);
                    editor.apply();
                    Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show();
                }
            }
        });