我想在点击按钮时显示在toast msg的帮助下计算的总数,但每次点击它我得到0.0可以帮助我解决这个问题吗?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s, s1, s2, s3, s4, s5, s6;
String result;
final String disp;
int a, a1, a2, a3, a4, a5, a6;
double total = 0;
double price;
Button button = (Button) findViewById(R.id.button);
CheckBox opt = (CheckBox) findViewById(R.id.opt);
CheckBox opt1 = (CheckBox) findViewById(R.id.opt1);
CheckBox opt2 = (CheckBox) findViewById(R.id.opt2);
CheckBox opt3 = (CheckBox) findViewById(R.id.opt3);
CheckBox opt4 = (CheckBox) findViewById(R.id.opt4);
CheckBox opt5 = (CheckBox) findViewById(R.id.opt5);
CheckBox opt6 = (CheckBox) findViewById(R.id.opt6);
EditText oid = (EditText) findViewById(R.id.oid);
EditText oid1 = (EditText) findViewById(R.id.oid1);
EditText oid2 = (EditText) findViewById(R.id.oid2);
EditText oid3 = (EditText) findViewById(R.id.oid3);
EditText oid4 = (EditText) findViewById(R.id.oid4);
EditText oid5 = (EditText) findViewById(R.id.oid5);
EditText oid6 = (EditText) findViewById(R.id.oid6);
if (opt.isChecked()) {
price = 13.0;
s = opt.getText().toString();
a = Integer.parseInt(oid1.getText().toString());
total += (price * a);
}
if (opt1.isChecked()) {
price = 13.0;
s1 = opt1.getText().toString();
a1 = Integer.parseInt(oid2.getText().toString());
total += (price * a1);
}
if (opt2.isChecked()) {
price = 13.0;
s2 = opt2.getText().toString();
a2 = Integer.parseInt(oid3.getText().toString());
total += (price * a2);
}
if (opt3.isChecked()) {
price = 13.0;
s3 = opt3.getText().toString();
a3 = Integer.parseInt(oid4.getText().toString());
total += (price * a3);
}
if (opt4.isChecked()) {
price = 13.0;
s4 = opt4.getText().toString();
a4 = Integer.parseInt(oid5.getText().toString());
total += (price * a4);
}
if (opt5.isChecked()) {
price = 13.0;
s5 = opt5.getText().toString();
a5 = Integer.parseInt(oid5.getText().toString());
total += (price * a5);
}
if (opt6.isChecked()) {
price = 13.0;
s6 = opt6.getText().toString();
a6 = Integer.parseInt(oid6.getText().toString());
total += (price * a6);
}
result = String.valueOf(total);
disp=result;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toastMsg(disp);
}
});
}
public void toastMsg(String result) {
Toast toast = Toast.makeText(this, result, Toast.LENGTH_LONG);
toast.show();
}
}
我尝试删除final关键字,因为这可能会阻止我将值更新为字符串但没有成功。 我很困惑如何继续。
答案 0 :(得分:0)
在log cat中打印结果变量并查看它是否包含任何值...我认为您的结果变量不包含任何值。你可以显示吐司而不将双重转换为字符串.. 如
public static void changeTabsAttributes(TabLayout tabLayout, int textSize) {
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
//set the background somewhere here?
((TextView) tabViewChild).setTextSize(textSize);
}
}
}
}
尝试这样做..将您的计算代码粘贴到按钮单击
Toast toast = Toast.makeText(this, total+"", Toast.LENGTH_LONG);
toast.show();
答案 1 :(得分:0)
private void calcResult()
{
String disp;
if (opt.isChecked()) {
price = 13.0;
s = opt.getText().toString();
a = Integer.parseInt(oid1.getText().toString());
total += (price * a);
}
if (opt1.isChecked()) {
price = 13.0;
s1 = opt1.getText().toString();
a1 = Integer.parseInt(oid2.getText().toString());
total += (price * a1);
}
if (opt2.isChecked()) {
price = 13.0;
s2 = opt2.getText().toString();
a2 = Integer.parseInt(oid3.getText().toString());
total += (price * a2);
}
if (opt3.isChecked()) {
price = 13.0;
s3 = opt3.getText().toString();
a3 = Integer.parseInt(oid4.getText().toString());
total += (price * a3);
}
if (opt4.isChecked()) {
price = 13.0;
s4 = opt4.getText().toString();
a4 = Integer.parseInt(oid5.getText().toString());
total += (price * a4);
}
if (opt5.isChecked()) {
price = 13.0;
s5 = opt5.getText().toString();
a5 = Integer.parseInt(oid5.getText().toString());
total += (price * a5);
}
if (opt6.isChecked()) {
price = 13.0;
s6 = opt6.getText().toString();
a6 = Integer.parseInt(oid6.getText().toString());
total += (price * a6);
}
result = String.valueOf(total);
disp=result;
toastMsg(disp);
}
创建一个我的答案中写的方法,其中包含计算代码,然后在按钮点击事件中调用该方法,如下所示,
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calcResult()
}
});
并且不要使disp最终使它成为calcResult方法的局部变量。
答案 2 :(得分:0)
我尝试在我的方法中添加所有计算,然后在按下按钮时调用它,当我运行应用程序时崩溃