Java 在Android Studio中生成变量final
当我试图使变量A(字符串)等于e(来自纯文本)时 这将出现在错误行中(在下面的代码中): 我不知道如何让“e”成为决赛Java 在Android Studio中生成变量final,java,android,Java,Android,当我试图使变量A(字符串)等于e(来自纯文本)时 这将出现在错误行中(在下面的代码中): 我不知道如何让“e”成为决赛 public void MORSE(View v) { EditText e=(EditText)findViewById(R.id.text); TextView T=(TextView)findViewById(R.id.translation); Button TRAD=(Button) findViewById(
public void MORSE(View v)
{
EditText e=(EditText)findViewById(R.id.text);
TextView T=(TextView)findViewById(R.id.translation);
Button TRAD=(Button) findViewById(R.id.translate);
TRAD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
String A;
A=""+e; //HERE IS THE ERROR.
}
});
}
您正在尝试向EditText添加字符串:
A=""+e;
这是办不到的。您可能希望将字符串添加到字符串中,而不是:
A = "" + e.getText().toString();
不能将e分配给,因为e是edittext,而a是字符串。这是一个java问题,我建议您在进入android之前先从Newboston的教程中学习java。 无论如何,你需要使用
A = e.getText();
不需要使用final关键字的final变量
在这里使用最后一个关键字会导致问题。最后一个变量无法编辑,也不是解决方案,因为该变量将保存该EditText的文本表示形式,而不是实际文本。不过,为了完整性起见,最后一个变量表示为:
final String finalString = "This string will now for ever hold this value. This can't be changed.";
e是您的
视图
它不是文本。使用e.getText()final EditText e=(EditText)findViewById(R.id.text)代码>
final String finalString = "This string will now for ever hold this value. This can't be changed.";