我有一个EditText,一个textView和一个按钮,
触摸按钮时,我需要使用EditText的值设置textView, 但是我有一个变量没有得到认可[对不起这里的机器人noob]
public class XynthaCalcTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//buttons
benefixPaButtonPressed();
benefixAdultButtonPressed();
xynthaButtonPressed();
calculateButtonPressed();
//textField == EditText
final EditText et;
et = (EditText)findViewById(R.id.editText1);
}
public void calculateButtonPressed() {
// TODO Auto-generated method stub
Button button = (Button) findViewById(R.id.result);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("myTag", "result biatch");
String text=et.getText().toString();
Toast msg = Toast.makeText(getBaseContext(),text, Toast.LENGTH_LONG);
msg.show();
}
});
}
}
所以与String text=et.getText().toString();
一致
我有错误et cannot be resolved
所以...... 如何声明我的编辑文本,以便按钮类可以看到它?
非常感谢!
答案 0 :(得分:2)
make et a field,拥有最终修饰符对我来说毫无意义。
public class XynthaCalcTestActivity extends Activity {
private EditText et;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//buttons
benefixPaButtonPressed();
benefixAdultButtonPressed();
xynthaButtonPressed();
calculateButtonPressed();
//textField == EditText
et = (EditText)findViewById(R.id.editText1);
}
public void calculateButtonPressed() {
// TODO Auto-generated method stub
Button button = (Button) findViewById(R.id.result);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("myTag", "result biatch");
String text=et.getText().toString();
Toast msg = Toast.makeText(getBaseContext(),text, Toast.LENGTH_LONG);
msg.show();
}
});
}
}
答案 1 :(得分:1)
将Edittext字段“et”定义为类变量。
答案 2 :(得分:1)
试试这段代码,它可以帮到你......
public class XynthaCalcTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//buttons
// benefixPaButtonPressed();
// benefixAdultButtonPressed();
// xynthaButtonPressed();
// calculateButtonPressed();
//textField == EditText
final EditText editText = (EditText)findViewById(R.id.editText1);
final TextView textView = (TextView)findViewById(R.id.textview);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String text = editText.getText().toString();
textView.setText(text);
Toast.makeText(XynthaCalcTestActivity.this,text, Toast.LENGTH_LONG).show();
}
});
}
}