Android-How To Make the Text display
I am new To android.I have Some Code..I have
Simple.java :
public class Simple extends Activity {
/** Called when the activity is first created. */
Button show;
TextView view;
EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show=(Button)findViewById(R.id.show);
view=(TextView)findViewById(R.id.view);
edit=(EditText)findViewById(R.id.edit);
show.setOnClickListener(new OnClickListener(){
public void onClick(View view){
show();
}
});
}
public void show(){
String text=edit.getText().toString();
view.setText(text);
Intent t=new Intent(this,Show.class);
startActivity(t);
}
}
When i tried To Display the text in the same activity it works... I am trying to pass the text Which i typed in EditText and to display it to Show.class
the code for Show.class
public class Show extends Activity {
private Simple simple;
TextView text1;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
back=(Button)findViewById(R.id.button);
text1=(TextView)findViewById(R.id.then);
back.setOnClickListener(new OnClickListener(){
public void onClick(View view){
开发者_JAVA百科 start();
}
});
}
public void start(){
String t=simple.edit.getText().toString();
text1.setText(t);
Intent t=new Intent(this,Simple.class);
startActivity(t);
}
}
I tried this one I am not able to Display the text which i gave in editText in Simple.java. I know it is basic but I do know. So Please Help me out.Thanks in Advance..
Try adding this to show():
Intent t = new Intent(this, Simple.class);
t.putExtra("editText", text);
startActivity(t);
Then in your Show class' start() method, use:
Intent t = getIntent();
Bundle data = t.getExtras();
text1.setText(data.getString("editText"));
I have not tested this (and am slightly confused by your implementation...) but the putExtra and getExtra functions are what you will likely wish to use.
public class Intents extends Activity {
/** Called when the activity is first created. */
EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
Button button1 = (Button) findViewById(R.id.start1);
button1.setOnClickListener(activity2);
edit=(EditText)findViewById(R.id.edit);
Button show=(Button)findViewById(R.id.show);
show.setOnClickListener(activity3);
}
private OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intents.this,
startactivity1.class));
}
};
private OnClickListener activity2 = new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intents.this,
startactivity2.class));
}
};
private OnClickListener activity3 = new OnClickListener() {
public void onClick(View v) {
String text=edit.getText().toString();
Intent t = new Intent(Intents.this, startactivity3.class);
t.putExtra("editText", text);
startActivity(t);
//startActivity(new Intent(Intents.this,
// startactivity3.class));
}
};
}
public class startactivity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actv3);
// Button ok = (Button)findViewById(R.id.but);
TextView text1=(TextView)findViewById(R.id.vi);
Intent t = getIntent();
Bundle data = t.getExtras();
text1.setText(data.getString("editText"));
}
}
Don't forget to add your new activity in android manifest file. go to your current application file click Application and add your new activity in my case i will add showactivity3 in your case you have to activity Show in your manifest. Try this code its tested
精彩评论