android导航屏幕之间的值

时间:2012-05-02 07:32:23

标签: android

在我的应用程序中,我想在单击按钮(在第一个屏幕中)时调用Web服务。在第一个屏幕中,我将输入一个输入。 基于该输入值,Web服务必须起作用。单击按钮后,我调用了一个新的intent,并调用了Web服务,但是如何将值从1个屏幕传递给其他人。

我的代码:

这是按钮点击(第1个屏幕)

public void onClick(View v)
            {
                 // TODO Auto-generated method stub     
//              EditText medit;
//              EditText medit=(EditText)findViewById(R.id.editText1);
                 Intent myIntent = new Intent(v.getContext(), dispaly.class);
                 startActivity(myIntent);


}

这是网络服务(点击按钮后)

medit=(EditText)findViewById(R.id.editText1);
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);  
             SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                String weight = medit.getText().toString();
                String fromUnit = "Grams";
                String toUnit = "Kilograms";

                PropertyInfo weightProp =new PropertyInfo();
                weightProp.setName("Weight");
                weightProp.setValue(weight);
//              weightProp.setValue(medit.toString());
                weightProp.setType(double.class);
                request.addProperty(weightProp);

                PropertyInfo fromProp =new PropertyInfo();
                fromProp.setName("FromUnit");
                fromProp.setValue(fromUnit);
                fromProp.setType(String.class);
                request.addProperty(fromProp);

                PropertyInfo toProp =new PropertyInfo();
                toProp.setName("ToUnit");
                toProp.setValue(toUnit);
                toProp.setType(String.class);
                request.addProperty(toProp);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);  
                try
                {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                    Log.i("myApp", response.toString());  
                    TextView tv = new TextView(this);
                    tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
                    setContentView(tv); 

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }.

2 个答案:

答案 0 :(得分:0)

您需要在调用intent时使用putExtra并在另一个屏幕中获取该意图。

例如你可以这样打电话。

 Intent myIntent = new Intent(this, PlayVideoActivity.class);
    myIntent.putExtra("KeyName", value);
    startActivity(myIntent);

现在,当另一项活动获得这样的价值时。

 String value=getIntent().getStringExtra("Keyname");

这是一个使用示例,您可以使用putExtra支持的类型for intent。

答案 1 :(得分:0)

有两种方法可以做到这一点。你可以使用putExtra或共享偏好   的 SharedPreferences:
  编辑共享偏好设置中的数据
       SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
       SharedPreferences.Editor editor = preferences.edit();
       editor.putString( “姓名”, “测试”);
       editor.commit();

从共享首选项中检索数据

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);    
  String name = preferences.getString("Name","");    
  if(!name.equalsIgnoreCase(""))  
  {   
      name = name+"  Sethi";  /* Edit the value here*/  
  }

OR

**putExtra:**

**pass data using putExtra at the time of calling intent.**

Intent myIntent = new Intent(this, PlayVideoActivity.class);
myIntent.putExtra("KeyName", value);
startActivity(myIntent);

**Retrive data  from other activity**

String value=getIntent().getStringExtra("Keyname");