当我点击它时我想要按钮出现一个对话框,我从该对话框中读取数据。在对话框中,我只有EditText用于数据。我的数据是数字。 这是我的代码:
Button mSaveButton = (Button) findViewById(R.id.saveButton);
mSaveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(ReportActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_for_percent , null);
EditText savingsDialog = (EditText) mView.findViewById(R.id.percentEditText);
mBuilder.setView(mView)
.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Context context = getApplicationContext();
Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog dialog = mBuilder.create();
dialog.show();
//if (savingsDialog.getText().toString().length() > 0) { I tried this if, but I receive empty data
SharedPreferences savings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = savings.edit();
editor.putString("percent", savingsDialog.getText().toString());
editor.commit();
updateSavingsView(Integer.parseInt(savingsDialog.getText().toString()));// here I want to put my data in method which update my TextView
//}
}
});
方法(如果需要)
public void updateSavingsView(int savings){
TextView savingsTextView = (TextView) findViewById(R.id.saverTextView);
savingsTextView.setText("Save " + savings + "% per month");
TextView monthlySavings = (TextView) findViewById(R.id.savingsMoney);
if(savings == 0 || Integer.parseInt(income.getText().toString()) == 0 ) {
monthlySavings.setText("0");
}
else{
monthlySavings.setText(Integer.toString((Integer.parseInt(income.getText().toString()) * savings) / 100));
}
所以问题是当我运行没有if语句的程序时我有一个错误java.lang.NumberFormatException:对于输入字符串:“”。这是因为它读取空字符串并想要解析它。然后我添加if语句以避免此问题,但它不起作用。 我的问题是。我如何重写这个监听器以避免这些问题并存储正确的数据?
答案 0 :(得分:2)
将您的代码置于正侦听器
中 Button mSaveButton = (Button) findViewById(R.id.saveButton);
mSaveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(ReportActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_for_percent , null);
final EditText savingsDialog = (EditText) mView.findViewById(R.id.percentEditText);
mBuilder.setView(mView)
.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Context context = getApplicationContext();
Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show();
SharedPreferences savings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = savings.edit();
editor.putString("percent", savingsDialog.getText().toString());
editor.commit();
updateSavingsView(Integer.parseInt(savingsDialog.getText().toString()));
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog dialog = mBuilder.create();
dialog.show();
}
}
});