简体   繁体   English

单击按钮后如何更改变量的值并在不同的活动中获取该值

[英]How to change value of variable after button click and get that value in different activity

I have a button. 我有一个按钮。 When I click the button, I want to change the value of the variable to 3. In another activity, I want to be able to get the value of the variable and do some computation based on that value. 单击按钮时,我想将变量的值更改为3。在另一个活动中,我希望能够获取变量的值并基于该值进行一些计算。

I have tried using getters and setters methods and just traditionally changing the values and nothing seems to work. 我尝试使用getters和setters方法,并且传统上只是更改值而似乎没有任何效果。

//MainActivity.java


        mButtonChoice2 = (Button)findViewById(R.id.choice2);

        mButtonChoice2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice2.getText() == "Three"){
                    setDays(3);
                    openActivity();

                }

     }


   public int getDays() {

        return days;
    }

    public void setDays(int value){
        days = value;
    }

    public void openActivity() {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }



//SecondActivity.java



public class SecondActivity extends AppCompatActivity {

    private TextView textview;
    private MainActivity obj = new MainActivity();

    Integer days = obj.getDays();

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        textview = findViewById(R.id.program);

        if (days == 3) {
            textview.setText("Days is 3");
        }
        else {
            textview.setText(days);
        }

I want the textview in the SecondActivity to update the textview text with "days is 3" 我希望SecondActivity中的textview用“ days is 3”更新textview文本

However, it just errors out because it is not being able to correctly receive the value of the integer days. 但是,它只是出错了,因为它不能正确接收整数天的值。

try the following updated code: 尝试以下更新的代码:

//MainActivity.java


        mButtonChoice2 = (Button)findViewById(R.id.choice2);

        mButtonChoice2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice2.getText() == "Three"){
                    setDays(3);
                    openActivity();

                }

     }


   public int getDays() {

        return days;
    }

    public void setDays(int value){
        days = value;
    }

    public void openActivity() {
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra ("days",days);
        startActivity(intent);
    }



//SecondActivity.java



public class SecondActivity extends AppCompatActivity {

    private TextView textview;
    private MainActivity obj = new MainActivity();

   // Integer days = obj.getDays();
   int days=0;

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

       Intent intent=getIntent();days=intent.getIntExtra ("days",0);


        textview = findViewById(R.id.program);

        if (days == 3) {
            textview.setText("Days is 3");
        }
        else {
            textview.setText(days);
        }

Reference : https://developer.android.com/reference/android/content/Intent https://www.dev2qa.com/passing-data-between-activities-android-tutorial/ 参考: https : //developer.android.com/reference/android/content/Intent https://www.dev2qa.com/passing-data-between-activities-android-tutorial/

只需在一个活动中声明一个静态变量,然后在第二个活动中使用它即可。

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

相关问题 如何通过单击按钮更改变量的值 - How to change the value of a variable on the click of a button 单击按钮后,SingleValueEventListener不会获取值 - SingleValueEventListener do not get the value after button click 如何使用主活动中的按钮增加其他活动中的TextView值? - How to increment a TextView value in a different activity with a button from the Main Activity? 如何从不同的活动中获取 RadioGroup 值 - How to get RadioGroup value from different Activity 如何将按钮单击时的 int 值从一个活动传递到另一个活动? - How to pass int value on button click from one activity to another? 如何通过在 android 工作室中单击按钮从另一个活动中更改活动中的 AsyncTask 变量? - How to change a variable of an AsyncTask in an activity from another activity by a button click in android studio? 如何在单击按钮时将类型按钮的输入值更改为小写 - How to change an input value of type button to lowercase on button click 多次计算后如何更改变量的值 - How to change the value of a variable after several calculations 每次单击按钮时如何将值写入不同的文本字段? - How to write value to different textfield every time i click a button? 单击按钮后如何将值传递给 JPanel 类? - How to pass value to JPanel class after click button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM