gpt4 book ai didi

java - 无法从按钮监听器接收数据

转载 作者:行者123 更新时间:2023-11-29 19:07:34 26 4
gpt4 key购买 nike

我想制作按钮,当我点击它时会出现一个对话框,然后我从该对话框中读取数据。在对话框中,我只有 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: For input string: ""。这是因为它读取空字符串并想要解析它。然后我添加 if 语句来避免这个问题,但它不起作用。我的问题是。我如何重写此监听器以避免这些问题并存储正确的数据?

最佳答案

将你的代码放在积极的监听器中

    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();
}
}
});

关于java - 无法从按钮监听器接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46613102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com