简体   繁体   English

从单选按钮更改按钮的onclicklistener

[英]Changing onclicklistener for a button from radio buttons

Hi im trying to make 3 radio buttons change onclick listener for a button each radiobutton sets its own on click listener for that button. 嗨,我试图使3个单选按钮更改按钮的onclick侦听器,每个单选按钮为该按钮设置自己的onclick侦听器。 but i get (MainActivity is not an enclosing class) 但是我得到了(MainActivity不是封闭类)

Note: the radiobuttons are in settings activity and the button is in mainactivity. 注意:单选按钮处于设置活动中,而按钮处于维护活动中。

Settings 设定值

public class Settings extends MainActivity {

private RadioGroup radioGroup;
private RadioButton radioButtonpc;
private RadioButton radioButtonps;
private RadioButton radioButtonxb;
public Button settings;
public Button cheatsactivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    cheatsactivity = (Button) findViewById(R.id.cheats_activity);
    settings = (Button) findViewById(R.id.setting_btn);
    radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
    radioButtonpc = (RadioButton) findViewById(R.id.radioButton1);
    radioButtonxb = (RadioButton) findViewById(R.id.radioButton2);
    radioButtonps = (RadioButton) findViewById(R.id.radioButton3);

    radioButtonpc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, PC.class);
                    startActivity(intent);
                }
            });
        }
    });

    radioButtonxb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, XBOX.class);
                    startActivity(intent);
                }
            });
        }
    });
    radioButtonps.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, PS.class);
                    startActivity(intent);
                }
            });
        }
    });
}




}

Update It now gives me java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference whenever i clicked on the radio button Im trying to change to make the user change the onclick listener for a button in main activty from the settings activity any solution? 更新 ,现在给我java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference whenever i clicked on the radio button尝试java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference whenever i clicked on the radio button更改以使用户从设置活动中更改主要活动中按钮的onclick侦听器有什么解决方案?

You need to use a living activitys context as the first parameter of new Intent . 您需要将生活活动上下文用作new Intent的第一个参数。

Try changing MainActivity.this to Settings.this in your intents, eg 尝试将意图中的MainActivity.this更改为Settings.this ,例如

Intent intent = new Intent(Settings.this, PC.class);

Write Settings.this instead of MainActivity.this like @howdoidothis said. 像@howdoidothis这样,写Settings.this而不是MainActivity.this And I think you have mistakenly used onClickListener instead of onCheckChangedListener . 我想你已经错误地使用onClickListener代替onCheckChangedListener See the following example: 请参见以下示例:

rd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        Intent intent = new Intent(Settings.this, PC.class);
        startActivity(intent);
    }
});

First, always follow re-usability because you have used Radio group and you applying intent from Radio button with another button click. 首先,请始终遵循可重用性,因为您已经使用了单选按钮组,并且单击了单选按钮并再次单击就应用了意图。

Once go with below code snippet,if still not work then update here, 使用以下代码段后,如果仍然无法使用,请在此处进行更新,

radioGroup 
        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.d("chk", "id" + checkedId);

                if (checkedId == R.id.radioButton1) {
                    //some code
                     Intent intent = new Intent(Settings .this, PC.class);
                    startActivity(intent);
                } else if (checkedId == R.id.radioButton2) {
                    //some code
                     Intent intent = new Intent(Settings .this, XBOX.class);
                    startActivity(intent);

                }else if (checkedId == R.id.radioButton3) {
                    //some code
                      Intent intent = new Intent(Settings .this, PS.class);
                    startActivity(intent);
                }

            }

        }); 

Try This Code 试试这个代码

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup radioGroup, int i) {
                        if (i == radioButtonpc.getId()) {
                            //your code
                        }
                        if (i == radioButtonxx.getId()) {
                            //your code
                        }
                        if (i == radioButton.getId()) {
                            //your code
                        }
                        if (i == radioButtonab.getId()) {
                            //your code
                        }
                    }
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM