如何在另一个活动中显示从一个活动传递的值?

huangapple go评论77阅读模式
英文:

How to display values passing from one activity in another activity?

问题

I'm passing 2 values of Number1 and Number2 from FirstActivity to SecondActivity here. In the SecondActivity I display those two numbers and then calculate the sum of it after clicking 'plus' button and display the sum in a text view.

public class FirstActivity extends AppCompatActivity {

    Button OkButton;
    EditText et_number1;
    EditText et_number2;
    String number1;
    String number2;

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

        OkButton = (Button) findViewById(R.id.ok_btn);
        et_number1 = (EditText) findViewById(R.id.ac1pt1);
        et_number2 = (EditText) findViewById(R.id.ac1pt2);
        number1 = et_number1.getText().toString();
        number2 = et_number2.getText().toString();

        OkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(FirstActivity.this, "Ok button clicked", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                intent.putExtra("EXTRAN1", number1);
                intent.putExtra("EXTRAN2", number2);
                startActivity(intent);
            }
        });
    }
}

public class SecondActivity extends AppCompatActivity {

    String no1;
    String no2;

    Button plus;
    Button minus;
    Button multiply;
    Button divide;
    TextView answer;
    TextView tv_number1;
    TextView tv_number2;

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

        plus = (Button) findViewById(R.id.plus);
        minus = (Button) findViewById(R.id.minus);
        multiply = (Button) findViewById(R.id.multiply);
        divide = (Button) findViewById(R.id.division);
        answer = (TextView) findViewById(R.id.actv3);

        Intent i = getIntent();
        no1 = i.getStringExtra("EXTRAN1");
        no2 = i.getStringExtra("EXTRAN2");

        tv_number1 = (TextView) findViewById(R.id.ac2tv1);
        tv_number2 = (TextView) findViewById(R.id.ac2tv2);

        tv_number1.setText(no1);
        tv_number2.setText(no2);

        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addition();
            }
        });
    }

    public void addition() {
        double n1 = Double.parseDouble(tv_number1.getText().toString());
        double n2 = Double.parseDouble(tv_number2.getText().toString());
        double result = n1 + n2;
        String r = Double.toString(result);
        answer.setText(r);
    }
}

Even though there's no error showing in the code, when I run the app, the two values of number 1 and number 2 are not displaying, and the calculation also not working. How am I supposed to correct that?

英文:

I'm passing 2 values of Number1 and Number2 from FirstActivity to SecondActivity here.In the SecondActivity I display those two numbers and then calculate the sum of it after clicking 'plus' button and display the sum in a text view.

public class FirstActivity extends AppCompatActivity {
Button OkButton;
EditText et_number1;
EditText et_number2;
String number1;
String number2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
OkButton = (Button) findViewById(R.id.ok_btn);
et_number1 = (EditText) findViewById(R.id.ac1pt1);
et_number2 = (EditText) findViewById(R.id.ac1pt2);
number1 = et_number1.getText().toString();
number2 = et_number2.getText().toString();
OkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(FirstActivity.this,"Ok button clicked",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("EXTRAN1",number1);
intent.putExtra("EXTRAN2",number2);
startActivity(intent);
}
});
}
}
public class SecondActivity extends AppCompatActivity {
String no1;
String no2;
Button plus;
Button minus;
Button multiply;
Button divide;
TextView answer;
TextView tv_number1;
TextView tv_number2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.division);
answer = (TextView) findViewById(R.id.actv3);
Intent i = getIntent();
no1= i.getStringExtra("EXTRAN1");
no2= i.getStringExtra("EXTRAN2");
tv_number1 = (TextView ) findViewById(R.id.ac2tv1);
tv_number2 = (TextView ) findViewById(R.id.ac2tv2);
tv_number1.setText(no1);
tv_number2.setText(no2);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addition();
}
});
}
public void addition(){
double n1 = Double.parseDouble(tv_number1.getText().toString());
double n2 = Double.parseDouble(tv_number2.getText().toString());
double result = n1 +n2;
String r = Double.toString(result);
answer.setText(r);
}
}

Eventhough there's no error showing in the code, when I run the app the two values of number 1 and number 2 are not displaying and the calculation also not working. How am I supposed to correct that?

答案1

得分: 1

你已将变量设置在按钮点击外部。
而且你的变量将没有值。
你应该将下面的代码复制到你的按钮点击监听器代码中。

OkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(FirstActivity.this, "Ok按钮被点击", Toast.LENGTH_SHORT).show();
number1 = et_number1.getText().toString();
number2 = et_number2.getText().toString();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("EXTRAN1", number1);
intent.putExtra("EXTRAN2", number2);
startActivity(intent);
}
});
英文:

you have set the Variable out of the button click.
and your Variable will be without value.
you should coppy below code to your button click listener code.

    OkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(FirstActivity.this,"Ok button clicked",Toast.LENGTH_SHORT).show();
number1 = et_number1.getText().toString();
number2 = et_number2.getText().toString();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("EXTRAN1",number1);
intent.putExtra("EXTRAN2",number2);
startActivity(intent);
}
});

huangapple
  • 本文由 发表于 2020年8月13日 23:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63398878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定