这是我的代码。我收到The method getText() is undefined for the type String
并且无法自己解决。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
EditText ETtitle = (EditText) findViewById(R.id.ETtitle);
EditText ETcontent = (EditText) findViewById(R.id.ETcontent);
button.setOnClickListener(new buttonOnClickListener());
}
public class buttonOnClickListener implements View.OnClickListener {
public void onClick(View v) {
String ETtitle = ETtitle.getText().toString();
}
}
}
答案 0 :(得分:2)
您正在本地重新定义ETtitle
更改
String ETtitle = ETtitle.getText().toString();
到
String editTextContent = ETtitle.getText().toString();
答案 1 :(得分:0)
更改
String ETtitle = ETtitle.getText().toString();
到
String ETtitleStr = ETtitle.getText().toString();
因为 ETtitle 已定义为edittext
并将EditText ETtitle
定义为类成员。
即。只需将代码重写为
即可public class MainActivity extends Activity {
private EditText ETtitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
ETtitle = (EditText) findViewById(R.id.ETtitle);
EditText ETcontent = (EditText) findViewById(R.id.ETcontent);
button.setOnClickListener(new buttonOnClickListener());
}
public class buttonOnClickListener implements View.OnClickListener {
public void onClick(View v) {
String ETtitleStr = ETtitle.getText().toString();
}
}
}