【问题标题】:Displaying another activity instead MainActivity显示另一个活动而不是 MainActivity
【发布时间】:2017-01-03 20:37:19
【问题描述】:

如果在设置中,在标记特定项目的 RadioButtons 列表中,我如何显示另一个活动而不是主要活动? 例如: 如果选择 A - 显示活动 A。 如果选择 B - 显示活动 B。 如果选择 C ​​- 显示活动 C。

【问题讨论】:

  • 看看这个。希望对你有帮助stackoverflow.com/questions/2776116/…
  • 当你说“如果你选择 A - 显示活动 A。如果你选择 B - 显示活动 B”是指这些活动的 XML?
  • @We'reAllMadHere 是的
  • 为什么不使用片段并在单选按钮单击时替换它?这才是正确的方法。
  • 那我猜你可以使用类似Intent i = new Intent(that, Activity_B.class);startActivity(intent);的东西。它的作用是,当你想要的事情发生时,它会改变活动并从你当前的活动变为活动 B

标签: java android android-activity settings display


【解决方案1】:

首先,获取当前从单选组中选择了哪个按钮。获得当前单选按钮后,只需检查按钮的文本或标签并基于它启动活动。

试试这样的,

radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected
                // get selected radio button from radioGroup
                int selectedId = radioGroup.getCheckedRadioButtonId();
                // find the radio button by returned id
                radioButton = (RadioButton) findViewById(selectedId);

                // toggle views on radio button click
                if (radioButton.getText().toString().equals("Activity A") {
                   startActivity(new Intent(CurrentActivity.this, ActivityA.class))
                } 
                else if (radioButton.getText().toString().equals("Activity B")) {
                    startActivity(new Intent(CurrentActivity.this, ActivityB.class))
                } 
                else if (radioButton.getText().toString().equals("Activity C")){
                    startActivity(new Intent(CurrentActivity.this, ActivityC.class))
                }
            }
        });

【讨论】:

    【解决方案2】:

    您可以使用 sharedPreferences。如果选择了第一个单选按钮,只需将 A 存储在 sharedPreferences 中,如果选择了第二个,则将 B 存储在等等。 在创建intent时,可以查看CallingActivity SharedPreference中存储的内容,如果是默认值则调用main,否则使用switch case调用具体activity。

    SharedPreferences sharedPref = getSharedPreferences("CallingActivity", Context.MODE_PRIVATE);
    SharedPreferences.Editor sEditor = sharedPref.edit();
    sEditor.putString("ActivityName","A");
    sEditor.commit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多