0

I have two activities in my application say EMPID and INFO. In EMPID activity I have one EditText say empid and in another activity INFO I have another EditText Name.

I am able switch between these activities using Button in each activity.

But I want the value of empid to be shown when I switch to second activity and come back to first activity.

Can anyone help on these.

Thank You in advance.

4

3 回答 3

1

将您的代码更改为将数据从第一个活动发送到第二个活动:

@Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     /*Create new Intent Object, and specify class*/
    Intent intent = new Intent();  
    intent.setClass(EMPID.this,INFO.class);
    /*new Bundle Object, and put data in bundle object*/ 
     Bundle bundle = new Bundle();  
     bundle.putString("EMPID ","empid");   
    /*Put Bundle object i.e bundle in intent*/ 
    intent.putExtras(bundle);  
    EMPID.this.startActivity(intent);
@Override
public void onResume()
{
  //SET EDITVIEW VALUE HERE to EMPID
    super.onResume();
}

在活动信息中:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.receivermain);
    TextView txtvwreceived=(TextView)findViewById(R.id.txtviewsender);
    /* Intent To obtain the  bundle object from EMPID */
    Bundle bundle = this.getIntent().getExtras();  
    /* Obtain String from Intent  */
    if(bundle !=null)
    {
        /* Obtain Data from bundle object */ 
        String strdata = bundle.getString("EMPID");
        }
    }

第二种方法使用第一个活动(EMPID.class)中的startActivityForResult()覆盖onActivityResult方法来启动您的第二个活动(INFO.class)以设置 EditView 值。

于 2012-06-24T08:30:16.923 回答
0

Use startActivityForResult() when you call the second activity. This will maintain the first activity in its previous state and you can have the empid maintained when you switch back to the first activity.

If you want to send the value of empid to second activity you can use intent.putExtra().

于 2012-06-24T08:21:54.737 回答
0

您将需要putExtras并且getIntExtras

下面的片段会帮助你。

EMPID.java

Intent intent=new Intent(this,INFO.class);
intent.putExtra("id", empid);
startActivity(intent);

信息.java

int empId=getIntent().getIntExtra("id", 0);
于 2012-06-24T08:28:03.103 回答