i have a resulttextview that shows the result of a computation. I would like to pass the value of the this resulttextview so that it will show in a toast.
i have this code: Toast.makeText(MyCalcActivity.this,message, Toast.LENGTH_LONG).show()
;
but this code is showing the value of the firstnumberTxt where i type the first number to be calculated instead. :(
Button plusBtn = (Button) findViewById(R.id.plusButton1);
plusBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText inputOne = (EditText) findViewById(R.id.firstnumberTxt);
String message = inputOne.getText().toString();
EditText inputTwo = (EditText) findViewById(R.id.secondnumberTxt);
String message2 = inputTwo.getText().toString();
int first = Integer.parseInt(message);
int second = Integer.parseInt(message2);
int sum = first + second;
TextView resultTxt = (TextView) findViewById(R.id.resultTextview);
resultTxt.setText("Result is " + sum);
Toast.makeText(MyCalcActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
is there a way i can do this please?
I think you are a starter, and just copied the code without knowing it even. So let me get it straight forward to you.
//Actual toast
Toast.makeText(MyCalcActivity.this, message, Toast.LENGTH_LONG).show();
// Exaplaining toast
Toast.makeText(1, 2, 3).show();
In first parameter 1
, it is the activity where the toast will be shown. Like you see in your code, it is MyCalcActivity.this
which means it is your current activity because of the input of this
.
In the second parameter 2
, it is what you want to be shown. See in your code you have used message
and from your code, we can know that message is a string
that has the value of an editText
you have that is called inputOne
. But you don't want that, right? You want the value of the textView resultTxt
so why you are using message
?! Replace message
with resultText.getText()
to get the value of the textview
you want.
In the last parameter, which is 3
. It is the length of the toast message
. How long do you want it? That what it is for. In you code it is set for Long toast message which I think it is about 3 seconds. If you want a shorter one, use Toast.LENGTH_SHORT
or a customized duration.
So at the end, This is your wanted code.
Toast.makeText(MyCalcActivity.this, resultTxt.getText().toString(), Toast.LENGTH_LONG)
.show();
Sorry for taking too long, but loved to help you. We all started from the bottom. But please next time search before you ask, there are a lot of similar questions and answers explaining them like this.
Right now you're putting message into the toast, the value of "firstnumberTxt", instead of using the result that you just finished calculating.
All you need to do is use the result in the toast:
Toast.makeText(MyCalcActivity.this, sum, Toast.LENGTH_LONG).show();
You can also go the long way and get what is in the TextView:
Toast.makeText(MyCalcActivity.this, resultText.getText().toString(), Toast.LENGTH_LONG).show();
First you are calling message in the toast
Toast.makeText(MyCalcActivity.this, message, Toast.LENGTH_LONG).show();
Instead to display the value of sum in the toast do,
Toast.makeText(MyCalcActivity.this, sum, Toast.LENGTH_LONG).show();
Or if you want to display the value in the resultTextView, do
Toast.makeText(MyCalcActivity.this, resultText.getText().toString(), Toast.LENGTH_LONG).show();
Button plusBtn = (Button) findViewById(R.id.plusButton1);
plusBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText inputOne = (EditText) findViewById(R.id.firstnumberTxt);
String message = inputOne.getText().toString();
EditText inputTwo = (EditText) findViewById(R.id.secondnumberTxt);
String message2 = inputTwo.getText().toString();
int first = Integer.parseInt(message);
int second = Integer.parseInt(message2);
int sum = first + second;
TextView resultTxt = (TextView) findViewById(R.id.resultTextview);
resultTxt.setText(String.valueOf(sum));
Toast.makeText(MyCalcActivity.this,"Result is" + resultTxt, Toast.LENGTH_LONG).show();
}
});
}
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.