【问题标题】:Depending of clicked button to open different activity android根据点击的按钮打开不同的活动android
【发布时间】:2014-11-29 19:17:54
【问题描述】:

在我的开始(主)屏幕上,我有 3 个按钮。 Button_1Button_2Button_3。它们都打开相同的Activity B。所以在这里我需要根据在主屏幕上单击哪个按钮在第二次活动后打开不同的Activity C。我可以用 2 个按钮完成它,但是当我尝试使用带有 elseif 的第三个按钮时,它无法正常工作。这是我尝试的方法。 主屏幕

Button_1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("isButtonClicked",true);
        startActivity(intent);

    }
});

Button_2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("button", true);
        startActivity(intent);

    }
});
Button_3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("isButtonClicked",false);
        startActivity(intent);

    }
});

这里是SecondActivity

boolean isButton = getIntent().getBooleanExtra("isButtonClicked",false); 
boolean button = getIntent().getBooleanExtra("button",true); 
            if(isButton) 
            { 
                Intent newActivity = new Intent(SecondActivity.this, ThirdActivity.class); 
                newActivity.putExtra("Position", Position); 
                newActivity.putExtra("resultServer", resultServer); 

                newActivity.putExtra("text", MyArrList.get(position).get("text").toString()); 
                newActivity.putExtra("name", MyArrList.get(position).get("name").toString()); 
                startActivity(newActivity); 
            } 
            else if (button)
            { 

                Intent newActivity = new Intent(SecondActivity.this, FourthActivity.class); 
                newActivity.putExtra("Position", Position); 
                newActivity.putExtra("resultServer", resultServer); 
                newActivity.putExtra("id", MyArrList.get(position).get("id").toString());
                startActivity(newActivity); 
            } 
            else 
            {
                Intent newActivity = new Intent(SecondActivity.this, FifthActivity.class); 
                newActivity.putExtra("Position", Position); 
                newActivity.putExtra("resultServer", resultServer); 
                newActivity.putExtra("id", MyArrList.get(position).get("id").toString());
                startActivity(newActivity); 
            }

正如您在第二个活动之后看到的,所有 3 个按钮都相同,然后我想加载不同的。如果我把它放在if{}else{} 上,即两个按钮效果很好。但现在我在Button_2Button_3 上得到相同的结果。

【问题讨论】:

  • same result on button 2 and 3... 哪个区块?
  • elseif{}else{} ... 两个按钮都打开FifthActivity.class

标签: android android-intent android-activity


【解决方案1】:

刚刚发现:

boolean button = getIntent().getBooleanExtra("button",true);

应该总是返回true。因为它的默认值设置为true。所以你的elseif 将永远有效..如果Button_1 没有被点击!

修复:

boolean button = getIntent().getBooleanExtra("button",false);

【讨论】:

  • 好的,我该如何解决这个问题?
  • true更改为false作为默认值。
【解决方案2】:

你有:

boolean button = getIntent().getBooleanExtra("button",true);

所以在第 2 和第 3 个选项中,这个值将是真的,所以这就是它的原因。

改为:

boolean button = getIntent().getBooleanExtra("button",false); 

应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-12-03
    • 1970-01-01
    相关资源
    最近更新 更多