从edittext获取文本并将其存储到字符串中

时间:2013-07-19 13:30:17

标签: android android-edittext

我正在处理由edittext字段和复选框组成的应用程序。如果用户只选择了其中几个,那么如何从字段中获取这些文本值。

public class ExtraFields extends Activity{

    Button btnSubmit;

    int noOfFields=0;



    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes bar

        setContentView(R.layout.extra_fields);

        btnSubmit = (Button) findViewById(R.id.submit);

        btnSubmit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                noOfFields = 0;

                send();
            }
        });
    }




    protected void send(){

    }


}

3 个答案:

答案 0 :(得分:4)

喜欢这个

    cb= (CheckBox)findViewById(R.id.checkbox1);
     if(cb.isChecked())
EditText t = (EditText) findViewById(R.id.edit4);
String a = t.getText().toString();
     }

答案 1 :(得分:2)

单击复选框时可以获取文本

if(checkbox1.isChecked()) {
EditText et1 = (EditText) findViewById(R.id.edittext1);
String one = et1.getText().toString();   
}

答案 2 :(得分:0)

您可以从EditText获取文本,如下所示:

CheckBox check1 = (CheckBox) findViewById(R.id.checkbox1);
Checkbox check2 = (Checkbox) findViewById(R.id.checkbox2);
if (check1.isChecked()) {
    EditText myEditText = (EditText) findViewById(R.id.edit_text_1);
    String string1 = myEditText.getText().toString();
}
if (check2.isChecked()) {
    EditText myEditText2 = (EditText) findViewById(R.id.edit_text_2);
    String string2 = myEditText2.getText().toString();
}