我正在制作一个简单的计算器,我为 -/+ 按钮编写了以下代码:
float ab=-1;
this.txtScreen = (EditText) findViewById(R.id.txtScreen);
unary=(Button)findViewById(R.id.plusminus);
mod=(Button)findViewById(R.id.modul);
unary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtScreen.setText(ab*txtScreen);
}
});
并向我显示错误 * operator cannot apply to float。
最佳答案
试试这个(它需要字符串):
float ab=-1;
unary=(Button)findViewById(R.id.plusminus);
mod=(Button)findViewById(R.id.modul);
unary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtScreen.setText(String.valueOf(ab * Float.valueOf(txtScreen.getText().toString())));
}
});
编辑:在计算中从 txtScreen 获取值。
关于java - 运算符不能应用于 float ,'android.widget.EditText',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38802665/