我正在使用两个editText框,例如“table no”和“no of guest”。如果我填写editText框并在listView中选择一些项目然后转到下一个活动并再次返回上一个活动,其中EditText框可用,则值为空。我的要求是,当我回到上一个活动时,EditText显示我之前输入的数据。
发送EDITTEXT数据:
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//for (int i=0;i<GlobalClass.myval.length;i++){
//System .out.println("Clicked-->"+GlobalClass.myval[i]);
//}
String tno = e1.getText().toString();
int tn = Integer.parseInt(tno);
Intent i=new Intent(getApplicationContext(),TicketActivity.class);
Bundle b=new Bundle();
b.putInt("Table No:", tn);
i.putExtras(b);
String et= e2.getText().toString();
int et1 = Integer.parseInt(et);
Bundle be=new Bundle();
be.putInt("Guest:", et1);
i.putExtras(be);
startActivity(i);
}
});
第二项活动:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket);
t1=(TextView)findViewById(R.id.textView3);
t2=(TextView)findViewById(R.id.textView5);
l1=(ListView)findViewById(R.id.listView1);
or=(ImageButton)findViewById(R.id.imageButton7);
ho=(ImageButton)findViewById(R.id.imageButton4);
de=(ImageButton)findViewById(R.id.imageButton1);
pl=(ImageButton)findViewById(R.id.imageButton2);
mi=(ImageButton)findViewById(R.id.imageButton3);
pa=(ImageButton)findViewById(R.id.imageButton5);
pr=(ImageButton)findViewById(R.id.imageButton6);
// l1.setItemsCanFocus(false);
//l1.setFocusable(true);
//l1.setClickable(true);
//Get the Table no Value From Edit Text
Intent i1=getIntent();
Bundle b=i1.getExtras();
int num=b.getInt("Table No:");
pno=Integer.toString(num);
t1.setText(pno);
//Get The Guest Value From Edit Text
Intent i2=getIntent();
Bundle b1=i2.getExtras();
int num1=b1.getInt("Guest:");
pno1=Integer.toString(num1);
t2.setText(pno1);
// List<String> st=GlobalClass.myval;
//ArrayAdapter<String> ada = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1,st);
l1.setAdapter(new EfficientAdapter(TicketActivity.this));
l1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
st2=HomeActivity.select1[position];
Toast.makeText(getApplicationContext(), "Selected:" + st2, Toast.LENGTH_SHORT).show();
}
});
//GO TO PREV ACTIVITY:
ho.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent o2=new Intent(TicketActivity.this,HomeActivity.class);
startActivity(o2);
}
});
}
}
答案 0 :(得分:4)
在通话活动中
Intent i = new Intent(A.this, B.class);
i.putExtra("string", editText.getText());
startActivity(i);
在被调用的活动中,有一个String来获取额外的内容并在onStop()
时再次传递给它Bundle extras = getIntent().getExtras();
String str = extras.getString("string");
在家庭活动中:
public void onCreate(Bundle savedInstanceState){
....
if(getIntent().getExtras()!=null) {
Bundle extras = getIntent().getExtras();
if(extras.getInt("TableNo") != null && extras.getInt("Guest") != null) }
e1.setText(extras.getInt("TableNo").toString());
e2.setText(extras.getInt("Guest").toString());
}
}
....
}
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//for (int i=0;i<GlobalClass.myval.length;i++){
//System .out.println("Clicked-->"+GlobalClass.myval[i]);
//}
String tno = e1.getText().toString();
int tn = Integer.parseInt(tno);
String et= e2.getText().toString();
int et1 = Integer.parseInt(et);
Intent i=new Intent(getApplicationContext(),TicketActivity.class);
i.putExtras("TableNo", tn);
i.putExtras("Guest", et1);
startActivity(i);
}
});
第二项活动:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket);
t1=(TextView)findViewById(R.id.textView3);
t2=(TextView)findViewById(R.id.textView5);
l1=(ListView)findViewById(R.id.listView1);
or=(ImageButton)findViewById(R.id.imageButton7);
ho=(ImageButton)findViewById(R.id.imageButton4);
de=(ImageButton)findViewById(R.id.imageButton1);
pl=(ImageButton)findViewById(R.id.imageButton2);
mi=(ImageButton)findViewById(R.id.imageButton3);
pa=(ImageButton)findViewById(R.id.imageButton5);
pr=(ImageButton)findViewById(R.id.imageButton6);
// l1.setItemsCanFocus(false);
//l1.setFocusable(true);
//l1.setClickable(true);
//Get the Table no Value From Edit Text
Intent i1=getIntent();
Bundle b=i1.getExtras();
int num=b.getInt("TableNo");
pno=Integer.toString(num);
t1.setText(pno);
//Get The Guest Value From Edit Text
Intent i2=getIntent();
Bundle b1=i2.getExtras();
int num1=b1.getInt("Guest");
pno1=Integer.toString(num1);
t2.setText(pno1);
// List<String> st=GlobalClass.myval;
//ArrayAdapter<String> ada = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1,st);
l1.setAdapter(new EfficientAdapter(TicketActivity.this));
l1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
st2=HomeActivity.select1[position];
Toast.makeText(getApplicationContext(), "Selected:" + st2, Toast.LENGTH_SHORT).show();
}
});
//GO TO PREV ACTIVITY:
ho.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String tno = t1.getText().toString();
int tn = Integer.parseInt(tno);
String et= t2.getText().toString();
int et1 = Integer.parseInt(et);
Intent o2=new Intent(TicketActivity.this,HomeActivity.class);
i.putExtras("TableNo", tn);
i.putExtras("Guest", et1);
startActivity(o2);
}
});
}
}
另外,尝试逻辑命名变量。随机命名不是一个好的编程实践,可能会妨碍代码的可维护性。
注意:请纠正一些小错误(如果有的话)。我现在没有android sdk所以无法编译这段代码。
答案 1 :(得分:2)
转到下一个活动时保存EditText字段的值。然后在返回上一个活动时检索该值。两个步骤:
您需要覆盖onSaveInstanceState(Bundle savedInstanceState)并保存EditText字段的值:
@覆盖
public void onSaveInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(“MyString”,“EditText value”);
}
Bundle本质上是一种存储NVP(“名称 - 值对”)映射的方式,它将被传递到onCreate和onRestoreInstanceState,你可以在这里提取这样的值:
@覆盖
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
String myString = savedInstanceState.getString(“MyString”);
}
您通常使用此技术存储应用程序的实例值(选择,未保存的文本等)。
回答礼貌:Saving Android Activity state using Save Instance State
答案 2 :(得分:1)
声明一个Class并声明一个静态变量并将值存储在静态变量中 喜欢这个
public class globalconstant
{
public static String edtStr;
}
商店价值
globalconstant.edtStr = edt.getText().toString();
当你想要使用时
致电globalconstant.edtStr