Reputation: 9555
How do I get the value of my EditText that the user typed in from the function that was called when the onClick was pressed. I tried this below but its not working. Thanks for the help.
<EditText android:id="@+id/myEditT" />
<Button android:text="MY BUTTON" android:onClick="GetThis" />
public void GetThis(View view) {
EditText x = (EditText)parentView.findViewById( R.id.myEditT);
// alert the x variable
}
Upvotes: 1
Views: 8440
Reputation: 2719
In XML,
<EditText android:id="@+id/myEditT"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button android:id="@+id/myButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
In Java,
public void onCreate(Bundle saved){
super.onCreate(saved);
setContentView(R.layout.your_xml);
Button btn = (Button) findViewById(R.id.myButton);
EditText edtText = (EditText) findViewById(R.id.myEdit);
btn.setOnClickListener(new onClickListener(
public void onClick(View v){
String value = edtText.getText().toString();
}
));
}
Upvotes: 1
Reputation: 457
EditText x;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml);
x = (EditText)parentView.findViewById( R.id.myEditT);
public void GetThis(View view) {
String s=x.getText().toString();
}
Upvotes: 0
Reputation: 61
EditText x = (EditText)findViewById(R.id.myEditT);
public void GetThis(View view) {
String getEditTextValue = x.getText().toString();
Toast.makeText(getApplicationContext(), getEditTextValue, Toast.LENGTH_LONG).show();
}
Upvotes: 0
Reputation: 3585
public void GetThis(View view) {
EditText x = (EditText)view.findViewById(R.id.myEditT);
String edittextvalue = x.getText().toString();
}
Upvotes: 0
Reputation: 13761
EditText x = (EditText) findViewById(R.id.myEditT);
String your_text = x.getText().toString();
Upvotes: 1