对于String类型,方法getText()未定义。关于从EditText获取内容

时间:2014-07-04 07:46:09

标签: java android

这是我的代码。我收到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();
        }

    }
}

2 个答案:

答案 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();
        }

    }
}