我是Android的新手。你能教我如何在editText中分配一个变量吗?请参阅下面的示例代码。
EditText assignVariable = (EditText) findViewById(R.id.variable);
if(assignVariable == 12 ) {
Toast.makeText(getApplicationContext(), "Assign Varialbe Successfull", Toast.LENGTH_SHORT);
return true;
} else {
Toast.makeText(getApplicationContext(), "Not Successfully Assign!", Toast.LENGTH_SHORT);
return true;
}
答案 0 :(得分:1)
如果要在edittext中设置文本,则可以使用:
assignVariable.setText("your text goes here");
如果您需要从edittext检索文本,则需要将此代码置于按钮单击上。 (不仅仅是在EditText初始化之后。)
String assignedText = assignVariable.getText().toString();
并且您无法将String与==
运算符进行比较。您应该使用equals
或equalsIgnoreCase
if (assignedText.equals("12")) {
Toast.makeText(getApplicationContext(), "Assign Varialbe Successfull", Toast.LENGTH_SHORT);
return true;
} else {
Toast.makeText(getApplicationContext(), "Not Successfully Assign!", Toast.LENGTH_SHORT);
return true;
}
希望这有帮助。
答案 1 :(得分:0)
EditText assignVariable = (EditText) findViewById(R.id.variable);
if(assignVariable.getText().toString().equals("12") ) {
Toast.makeText(getApplicationContext(), "Assign Varialbe Successfull", Toast.LENGTH_SHORT);
return true;
} else {
Toast.makeText(getApplicationContext(), "Not Successfully Assign!", Toast.LENGTH_SHORT);
return true;
}