引用以编程方式创建的EditText的值

时间:2013-04-16 21:57:44

标签: java android android-edittext

我尝试过这两种方法在AsyncTask中创建EditText视图,然后在OnClickListener中引用它们的值。无论哪种方式我这样做,应用程序编译并运行正常,但字符串是空的。如何从EditTexts中获取值?

方法1:

public class OptionsActivity extends Activity {

public String strNick;
public String strEmail;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);

    OptionsTask task = new OptionsTask();
    task.execute(); 
}

View.OnClickListener clickHandler = new View.OnClickListener() {
    public void onClick(View v) {

        EditText etNick = (EditText) findViewById(8888);
        strEmail = etNick.getText().toString();
        EditText etEmail = (EditText) findViewById(9999);
        strEmail = etEmail.getText().toString();
    }
};

private class OptionsTask extends AsyncTask<Void, Void, Void> {

    protected Void doInBackground(Void... params) {
        //irrelevant stuff here
        }

    protected void onPostExecute(Void result) {
        super.onPostExecute(result); 

        LinearLayout ll = (LinearLayout)findViewById(R.id.options_activity_ll);

        EditText etNick = new EditText(OptionsActivity.this);
        etNick.setId(8888);
        etNick.setText(strNick);
        ll.addView(etNick, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        EditText etEmail = new EditText(OptionsActivity.this);
        etEmail.setId(9999);
        etEmail.setText(strEmail);
        ll.addView(etEmail, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        Button myButton = new Button(OptionsActivity.this);
        myButton.setOnClickListener(clickHandler);
        ll.addView(myButton, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        }
}

}

方法2:

public class OptionsActivity extends Activity {

public String strNick;
public String strEmail;
public EditText etNick;
public EditText etEmail;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);

    OptionsTask task = new OptionsTask();
    task.execute(); 
}

View.OnClickListener clickHandler = new View.OnClickListener() {
    public void onClick(View v) {

        strEmail = etNick.getText().toString();
        strEmail = etEmail.getText().toString();
        //strNick = String.valueOf(etNick.getText());  (also tried this)
        //strEmail = String.valueOf(etEmail.getText());
    }
};

private class OptionsTask extends AsyncTask<Void, Void, Void> {

    protected Void doInBackground(Void... params) {
        //irrelevant stuff here
        }

    protected void onPostExecute(Void result) {
        super.onPostExecute(result); 

        LinearLayout ll = (LinearLayout)findViewById(R.id.options_activity_ll);

        EditText etNick = new EditText(OptionsActivity.this);
        etNick.setText(strNick);
        ll.addView(etNick, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        EditText etEmail = new EditText(OptionsActivity.this);
        etEmail.setText(strEmail);
        ll.addView(etEmail, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        Button myButton = new Button(OptionsActivity.this);
        myButton.setOnClickListener(clickHandler);
        ll.addView(myButton, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        }
}

}

0 个答案:

没有答案