0

Question: I am trying to take the index of the selected radio button and use that in the onClick function.

Example:

    btnCalc.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
               if (getIndex(k) == 0) {
                      DO THIS
               }       
               if (getIndex(k) == 1) {
                      DO THAT
               }                
    });

I have the following code in my Android app:

    int POS; //global variable assigned right under MainActivity
    final RadioGroup rgTypeOfTrip = (RadioGroup) findViewById(R.id.rgTripType);
    Button btnCalc = (Button) findViewById(R.id.btnCalculate);

        btnCalc.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                  Toast.makeText(MainActivity.this, Integer.toString(getIndex(POS1)), Toast.LENGTH_SHORT).show(); //DOESN'T WORK
        });

        rgTypeOfTrip.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub

                // Method 1
                int pos=rgTypeOfTrip.indexOfChild(findViewById(checkedId));
                    getIndex(pos);
                Toast.makeText(MainActivity.this, String.valueOf(pos), Toast.LENGTH_SHORT).show();
            }
        });

public int getIndex(int POS1) {
    Toast.makeText(MainActivity.this, Integer.toString(POS1), Toast.LENGTH_SHORT).show(); // WORKS
    return POS1;
}

How can I achieve this line:

Toast.makeText(MainActivity.this, CALL FUNCTION GETINDEX() to get value, Toast.LENGTH_SHORT).show();

To call the function to get the value of k?

4

1 回答 1

2

假设您的问题是Toast这样的:“CALL FUNCTION GETINDEX() to get value”

Toast.makeText(MainActivity.this, Integer.toString(getIndex(int)), Toast.LENGTH_SHORT).show();

您传入返回的整数,因此您可以使用以下内容(根据我对您的代码的假设):

Toast.makeText(MainActivity.this, Integer.toString(int), Toast.LENGTH_S
于 2013-07-24T15:41:28.570 回答