我想将edittext百万像素的内容更改为宽度和高度文本框的产品,以制作百万像素计算器。
public void calculate()
{
EditText tb_widthpx = (EditText) findViewById(R.id.txtbox_widthpx);
EditText tb_heightpx = (EditText)findViewById(R.id.txtbox_heigthpx);
EditText tb_megapixel = (EditText) findViewById(R.id.txtbox_megapixel);
if (tb_widthpx.getText().toString() != "" && tb_heightpx.getText().toString() !="")
{
int produkt;
int width = Integer.parseInt(tb_widthpx.getText().toString());
int height = Integer.parseInt((tb_heightpx.getText().toString()));
produkt = width * height;
tb_megapixel.setText(String.valueOf(produkt));
}
答案 0 :(得分:1)
在Java
中你必须使用.equals()
,所以试试这个,
public void calculate()
{
EditText tb_widthpx = (EditText) findViewById(R.id.txtbox_widthpx);
EditText tb_heightpx = (EditText)findViewById(R.id.txtbox_heigthpx);
EditText tb_megapixel = (EditText) findViewById(R.id.txtbox_megapixel);
if (!tb_widthpx.getText().toString().equals("") && !tb_heightpx.getText().toString().equals(""))
{
int produkt;
int width = Integer.parseInt(tb_widthpx.getText().toString());
int height = Integer.parseInt((tb_heightpx.getText().toString()));
produkt = width * height;
tb_megapixel.setText(String.valueOf(produkt));
}
答案 1 :(得分:0)
您可以使用.equals()
来比较字符串。像这样:
if (!tb_widthpx.getText().toString().equals("") && !tb_heightpx.getText().toString().equals("")) {
int produkt;
int width = Integer.parseInt(tb_widthpx.getText().toString());
int height = Integer.parseInt((tb_heightpx.getText().toString()));
produkt = width * height;
tb_megapixel.setText(String.valueOf(produkt));
}
但为了获得更好的性能,您应该使用.length()
。像这样:
if (!tb_widthpx.getText().toString().length() == 0 && !tb_heightpx.getText().toString().length() == 0) {
int produkt;
int width = Integer.parseInt(tb_widthpx.getText().toString());
int height = Integer.parseInt((tb_heightpx.getText().toString()));
produkt = width * height;
tb_megapixel.setText(String.valueOf(produkt));
}