我不能從 EditText fr1中獲取數據。然後在fr1中輸入一些文本,點擊按鈕 btn 但是沒有任何反應。TextView 的結果還是空的。為什麼是空的呢?
public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fr1 = (EditText) findViewById(R.id.fraction1);
final TextView result = (TextView) findViewById(R.id.result);
Button btn = (Button) findViewById(R.id.getresult);
final String s = fr1.getText().toString();
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
result.setText(s);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
result.setText(getStrFromEditText());
}
});
private String getStrFromEditText() {
return fr1.getText().toString().trim();
}