简体   繁体   中英

Getting text from edittext and storing it to a string

I am working on application which consists of edittext fields along with checkboxes. If only few of them are selected by the user, then how to get those text values from the fields.

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(){

    }


}

Like this

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

You can get the text when checkbox is clicked

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

You can get text from an EditText as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM