【发布时间】:2014-08-04 10:00:20
【问题描述】:
所以我目前正在开发我自己的私人应用程序,我只想为我和一些朋友使用它。 好吧,我是德国人,所以我的英语可能不是最好的,希望您能原谅我。
现在我的问题是我想在我的选项菜单中设置当月的预算以跟踪。我通过使用带有按钮的 EditText 来做到这一点。
现在我想将在我的 EditText 中输入的这个字符串保存为一个字符串值和一个整数值,因为我想在我的 MainPage 上的 TextView 中显示该值并使用整数值来计算我得到的当前预算等等。
现在我向你们展示我的代码,希望你们能告诉我它有什么问题。 我试图在与我的 OptionsMenu 相关的 Options 类中获取我的值,然后尝试将值从我的 Options 类中获取到我的 Main 类中。
public class Options extends Activity {
Button GoBack, SetBudget;
private int Budget;
String BudgetString = "";
EditText BudgetEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
GoBack = (Button) findViewById(R.id.button2);
GoBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent GoBackToMain = new Intent("com.lunarrepublic.STARTINGPOINT");
startActivity(GoBackToMain);
}
});
SetBudget = (Button) findViewById(R.id.button8);
SetBudget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
}
但如果我尝试将我的“BudgetString”设置为可编辑,它也不起作用。 GoBack 按钮对于我的问题来说是不必要的,所以你不必仔细阅读它。
所以我希望你们明白我的问题是什么,也许可以帮助我解决它
提前致谢
【问题讨论】:
-
BudgetString = EditText.getText().toString(); -
Editable实际上不是String。您必须使用toString()转换为parseInt()可以接受的数据类型。 -
BudgetString = EditText.getText();,错了,改用你的对象BudgetEdit。还要在 try catch 中保留Budget = Integer.parseInt(BudgetString);以捕获任何数字格式异常。
标签: java android android-edittext setvalue