比较 2 个文本视图值
comparing 2 textview values
我有 2 个 TextView
Textview1 值“0”,Textview2 值“0.05”
试试这个代码
private TextView geting_id_one, geting_id_two;
{onCreate)
geting_id_one = (TextView) findViewById(R.id.Textview1);
geting_id_two= (TextView) findViewById(R.id.Textview2);
(button Click)
float one = geting_id_one.getText().toString();
float two = geting_id_two.getText().toString();
if (one>two){
Toast.makeText(MainActivity.this,"TextView1 Bigger than TextView2", Toast.LENGTH_SHORT).show();
}
if (one>two){
Toast.makeText(MainActivity.this,"TextView2 Bigger than TextView1", Toast.LENGTH_SHORT).show();
}
查看错误
E/Error In Thread: java.lang.NumberFormatException: For input string: "0.05"
0.05
无效 Integer
值
您应该使用 double
或 float
而不是 int
示例代码
// inside oncreate()
geting_id_one = (TextView) findViewById(R.id.Textview1);
geting_id_two= (TextView) findViewById(R.id.Textview2);
// inside button Click
double one = Double.parseDouble(geting_id_one.getText().toString());
double two = Double.parseDouble(geting_id_two.getText().toString());
// OR
float one = Float.parseFloat(geting_id_one.getText().toString());
float two = Float.parseFloat(geting_id_two.getText().toString());
您还可以通过比较两个文本视图的字符串值来比较它们的字符串值,例如:
if (text1.getText().toString().equals(text2.getText().toString())) {
//both are equal
} else {
//both have different value
}
您似乎正在尝试使用 Integer.parseInt()
方法解析 float
值。请改用 Double.parseDouble("your_number_string")
或 Float.parseFloat("your_number_string")
。如果需要,您可以将其转换为 integer
。
我有 2 个 TextView
Textview1 值“0”,Textview2 值“0.05”
试试这个代码
private TextView geting_id_one, geting_id_two;
{onCreate)
geting_id_one = (TextView) findViewById(R.id.Textview1);
geting_id_two= (TextView) findViewById(R.id.Textview2);
(button Click)
float one = geting_id_one.getText().toString();
float two = geting_id_two.getText().toString();
if (one>two){
Toast.makeText(MainActivity.this,"TextView1 Bigger than TextView2", Toast.LENGTH_SHORT).show();
}
if (one>two){
Toast.makeText(MainActivity.this,"TextView2 Bigger than TextView1", Toast.LENGTH_SHORT).show();
}
查看错误
E/Error In Thread: java.lang.NumberFormatException: For input string: "0.05"
0.05
无效 Integer
值
您应该使用 double
或 float
而不是 int
示例代码
// inside oncreate()
geting_id_one = (TextView) findViewById(R.id.Textview1);
geting_id_two= (TextView) findViewById(R.id.Textview2);
// inside button Click
double one = Double.parseDouble(geting_id_one.getText().toString());
double two = Double.parseDouble(geting_id_two.getText().toString());
// OR
float one = Float.parseFloat(geting_id_one.getText().toString());
float two = Float.parseFloat(geting_id_two.getText().toString());
您还可以通过比较两个文本视图的字符串值来比较它们的字符串值,例如:
if (text1.getText().toString().equals(text2.getText().toString())) {
//both are equal
} else {
//both have different value
}
您似乎正在尝试使用 Integer.parseInt()
方法解析 float
值。请改用 Double.parseDouble("your_number_string")
或 Float.parseFloat("your_number_string")
。如果需要,您可以将其转换为 integer
。