如何利用外部开关盒的价值?

时间:2018-11-13 22:29:22

标签: android

我是android studio的新手,并且有一个问题。根据我的代码,我如何使用iii。我想将它们加在一起并显示在tv3中,但始终为0。我知道我应该在TextView tv3之前做一些事情,但我并不专业。

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

RadioGroup gr1,gr2;

Button driver;

int i,ii,iii;

TextView tv1;
TextView tv2;
TextView tv3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gr1=(RadioGroup)findViewById(R.id.g1);
    gr2=(RadioGroup)findViewById(R.id.g2);

    tv1=(TextView)findViewById(R.id.tv1);
    tv2=(TextView)findViewById(R.id.tv2);
    tv3=(TextView)findViewById(R.id.tv3);

    driver=(Button) findViewById(R.id.driver);

    gr1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
{
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) 
{
            switch (checkedId){
                case R.id.r1:
                    i=1;
                    tv1.setText(String.valueOf(i));
                    break;
                case R.id.r2:
                    i=1-1;
                    tv1.setText(String.valueOf(i));
                    break;
            }


        }
    });
    gr2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
{
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) 
{
            switch (checkedId){
                case R.id.r3:
                    ii=1;
                    tv2.setText(String.valueOf(ii));
                    break;
                case R.id.r4:
                    ii=1-1;
                    tv2.setText(String.valueOf(ii));;
                    break;
            }

        }
    });


    tv3.setText(String.valueOf(iii));

    driver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (iii==2){
                Toast.makeText(MainActivity.this, "Complete", 
Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(MainActivity.this, "InComplete", 
Toast.LENGTH_SHORT).show();
            }

        }
    });

}


}

2 个答案:

答案 0 :(得分:0)

将您的tv3.setText(String.valueOf(iii));放在切换用例代码块之后。

如果执行任何时间切换条件,则tv3将显示新的iii值。如果要添加i和ii,则只需在setText()之前添加它们即可。

gr2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId) {
                case R.id.r3:
                    ii = 1;
                    tv2.setText(String.valueOf(ii));
                    break;
                case R.id.r4:
                    ii = 1 - 1;
                    tv2.setText(String.valueOf(ii));
                    break;
            }
            // add value
            iii = i + ii;
            tv3.setText(String.valueOf(iii));
        }
    });

答案 1 :(得分:0)

第一次创建活动时,iii始终为0,并且只有在您按下操作时才会出现

即使您单击单选按钮,iii的值仍为零,因为您没有在任何地方使用加法

gr1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
{
 @Override
 public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) 
 {
            switch (checkedId){
                case R.id.r1:
                    i=1;
                    tv1.setText(String.valueOf(i));
                    break;
                case R.id.r2:
                    i=1-1;
                    tv1.setText(String.valueOf(i));
                    break;
            }
       iii = i+ii;
       tv3.setText(String.valueOf(iii));//here you can check whether iii is zero if 0 don't set otherwise set text

        }
    });

gr2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
{
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) 
{
            switch (checkedId){
                case R.id.r3:
                    ii=1;
                    tv2.setText(String.valueOf(ii));
                    break;
                case R.id.r4:
                    ii=1-1;
                    tv2.setText(String.valueOf(ii));;
                    break;
            }
       iii = i+ii;
       tv3.setText(String.valueOf(iii));


        }
    });