简体   繁体   中英

How to use the variable of one method in another method in Java(android)

I am retrieving the value of a TextView like this

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calculation);
    TextView heading = (TextView) findViewById(R.id.paper_name);
    ......
}

I have another method in same class

@Override
public void onClick(View v) {   
    int BLACK_AND_WHITE_MULTIUPLIER = 4200;
    int COLOR_MULTIUPLIER = 6400;
    switch (v.getId()) {
        case R.id.btCalculate:
        int multiplier = rbColor.isChecked() ? COLOR_MULTIUPLIER : BLACK_AND_WHITE_MULTIUPLIER;
        int column = Integer.parseInt((String) spColumn.getSelectedItem());
        int inch = Integer.parseInt((String) spInch.getSelectedItem());
        tvAmount.setText((multiplier * column * inch) + "");
        break;
    }
}

I want to set the value of COLOR_MULTIUPLIER and BLACK_AND_WHITE_MULTIUPLIER based on value of heading which I have got from the onCreate method. Is it possible?

Move the TextView heading; so that it is a class variable not a local variable and then it will be accessible throughout your whole class.

It can then be intialized in onCreate and read and or updated in onClick

Declare your TextView at class level...

TextView heading;

protected void onCreate(Bundle savedInstanceState)
    {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculation);

        heading = (TextView) findViewById(R.id.paper_name);
        heading.setText("something");

            ......

then access it in onClick() method as below...

@Override
public void onClick(View v)
{   
     int BLACK_AND_WHITE_MULTIUPLIER = 4200;
     int COLOR_MULTIUPLIER = 6400;
    switch (v.getId())
    {
        case R.id.btCalculate:

            int multiplier = 0;

            if (heading.getText().toString.equals("something")) {

                multiplier = COLOR_MULTIUPLIER;

            } else {

                multiplier = BLACK_AND_WHITE_MULTIUPLIER;

            }

            //int multiplier = rbColor.isChecked() ? COLOR_MULTIUPLIER
            //        : BLACK_AND_WHITE_MULTIUPLIER;
            int column = Integer.parseInt((String) spColumn
                    .getSelectedItem());
            int inch = Integer.parseInt((String) spInch.getSelectedItem());
            tvAmount.setText((multiplier * column * inch) + "");
            break;
    }
}

Like I said in my comment , you just need to make heading a variable that belongs to the whole object, not just the onCreate method. You do this in the following way.

public class MyActivity extends Activity {

    TextView heading;  //Declared outside of onCreate

    protected void onCreate(Bundle savedInstanceState) {
        heading = (TextView) findViewById(R.id.paper_name); //Assigned inside of onCreate
    }
}

Then that variable becomes accessible inside your onClick

public void onClick(View v) {
    heading.getText(); //Or whatever you need to use it for
}

Declare your TextView heading outside of the onCreate method ie at class level. Inside onCreate assign it a value and Now in onClick method can access the varaible heading and get it's value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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