Aditya Reddy
Aditya Reddy

Reputation: 11

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

    }


}

Upvotes: 0

Views: 120

Answers (3)

TronicZomB
TronicZomB

Reputation: 8747

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

Upvotes: 0

Benil Mathew
Benil Mathew

Reputation: 1634

Like this

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

Upvotes: 4

Aravin
Aravin

Reputation: 4108

You can get the text when checkbox is clicked

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

Upvotes: 2

Related Questions