0

我正在使用两个 editText 框,例如“table no”和“no of guest”。如果我填充 editText 框并在 listView 中选择一些项目,然后转到下一个活动并再次返回上一个活动,其中 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);

    }
  });

      }

    }
4

3 回答 3

4

在调用活动中

Intent i = new Intent(A.this, B.class);
i.putExtra("string", editText.getText());
startActivity(i);

在被调用的活动中,有一个字符串来获取这个额外的并再次将它传递给 when 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,所以无法编译这段代码。

于 2012-09-04T06:45:42.787 回答
2

转到下一个活动时保存 EditText 字段的值。然后在您返回之前的活动时检索该值。两步:

  1. 您需要覆盖 onSaveInstanceState(Bundle savedInstanceState) 并保存 EditText 字段的值:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState)
    {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("MyString", "EditText value");
    }

  2. Bundle 本质上是一种存储 NVP(“名称-值对”)映射的方式,它将被传递给 onCreate 和 onRestoreInstanceState,您可以在其中提取如下值:

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
    super.onRestoreInstanceState(savedInstanceState);
    String myString = savedInstanceState.getString("MyString");
    }

您通常会使用这种技术来存储应用程序的实例值(选择、未保存的文本等)。

回答礼貌:使用 Save Instance State 保存 Android Activity 状态

于 2012-09-04T06:57:57.387 回答
1

声明一个类并声明一个静态变量并将值存储在静态变量中 像这样

public class globalconstant 
{ 
  public static String edtStr; 
}

储存价值

globalconstant.edtStr = edt.getText().toString();

当你想使用时,只需调用globalconstant.edtStr

于 2012-09-04T09:55:54.153 回答