问题
Basically I'm trying to get my EditText with a button "Ok" to display what was written to show up in a TextView output.
Once the "Ok" button is pressed it get the text what was entered in the EditText and display in the TextView.
Been searching around and can't find anything that works. Can anyone help?
Edit: 1) really appreciate the responds and help guys. 2) I think I"m asking this wrong. What I'm trying to do is creating an option where the user can add their own Text into an activity.
Edit: THANK YOU FOR THE HELP!!!!!!!!!!!!
回答1:
Get Value from EditText
editText.getText().toString();
Set value to TextView in Button Click
textView.setText(editText.getText().toString());
Update:
implement Button Onclick(...) like
Button negativeButton = (Button) findViewById(R.id.ButtonID);
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(editText.getText().toString());
}
});
回答2:
Use in your Activity in OnCreate()
Button _ok = (Button) findViewById(R.id.idofButton);
TextView result = (TextView ) findViewById(R.id.idofTextView);
EditText entrytext = (EditText ) findViewById(R.id.idofEditText);
_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result .setText(entrytext .getText().toString());
}
});
回答3:
Let's suppose the EditText is named as et, TextView is named as tv and your Button is named a as bt..
Now declare your EditText, TextView and Button, below your class name like this:
public class EditToText extends Activity{
EditText et;
TextView tv;
Button bt;
------
------
}
Now in your onCreate method and after setContentView do as mentioned below :
------
------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.edittext); //EditText is defined as edittext in xml
tv = (TextView) findViewById(R.id.textview); //TextView is defined as textview in xml
bt = (Button) findViewById(R.id.button); //Button is defined as button in xml
//When button is clicked
bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String txt = et.getText().toString(); //Get txt from et when button is clicked
tv.setText(txt); //Set text extracted from et in tv
}
});
}
------
------
Hope this Helps!
回答4:
activity_mail.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.stackedittext.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="ENTER TEXT HERE" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="62dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="42dp"
android:text="OK" />
</RelativeLayout>
MainActivity.java
package com.example.stackedittext;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button ok;
EditText edt;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok = (Button) findViewById(R.id.button1);
edt = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.textView1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = edt.getText().toString();
tv.setText(msg);
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
来源:https://stackoverflow.com/questions/27683877/how-do-i-get-a-edittext-to-display-in-my-textview