Reputation: 800
I have an editText, How do I get the text typed in it by the user ?
example, this is my editText
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="10">
</EditText>
and the user typed Hi, in my next Activity how do I make if the user typed Hi show in the next Activity Hello.. etc if the user typed How are you next activity it shows I'm fine ?
What do I use? and is it related to Database ?
Upvotes: 0
Views: 3474
Reputation: 724
Let's assume that your class's name is : Main
First declare a static public String :
public static String myText;
Now, in your OnCreate method type:
EditText et = (EditText) findViewById(R.id.editText3);
Finally, in you OnClick method type:
myText = getText().toString();
Intent x = new Intent(getApplicationContext(), NewActivity.class);
startActivity(x);
Now, in your next activity you can access the String only using :
Main.myText
For exemple:
TextView txt =(TextView) findViewById(R.id.textviewid);
txt.setText(Main.myText);
Upvotes: 1
Reputation: 42450
First, pick up the text in the current activity, i.e. the activity where the view hosts the EditText
component.
EditText txt = (EditText)findViewById(R.id.editText3);
String input = txt.getText().toString();
Now, add it as an extra data and call your new activity.
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("input",input);
startActivity(i);
In NewActivity
inside onCreate()
:
Intent i = getIntent();
String input = i.getStringExtra("input");
and, voila, you have the entered text in the new activity.
To set this to the new TextView
:
TextView txtView = (TextView)findViewById(R.id.yourTextView);
txtView.setText(input);
Upvotes: 3
Reputation: 15052
The simplest way to go would be use Intent
's getExtra()
and putExtra()
method, do a simple String
comparison and return your results.
Here's something to get you started : How do I get extra data from intent on Android? and How do I compare strings in Java?.
But from what you have written, seems you are trying to make a chatting application wherein your application responds to the messages typed in by the user. For this, I would recommend you to use AIML. It's a long long shot, but worth it.
Upvotes: 1