1

我有四项活动,即

  • Demo_tabActivity.java [主要活动]

  • Tabhost.java

下面两个activity是上面tabhost.java的标签

  • Tab_1.java

  • Tab_2.java

第一个活动(Demo_tabActivity.java)包含一个编辑文本和按钮。第二个(Tabhost.java)活动包含一个 Tabhost 小部件。第三个和第四个活动分别包含文本视图。

第一个活动将通过获取用户的输入来使用 Web 服务,并在选项卡主机(第二个活动)的第一个选项卡(第三个活动)上返回一些数据。

良好的网络服务消费运作良好并完美返回价值,

但是,问题是,它在单独的页面上显示结果,而不是在 tabhost 上显示。

Demo_tabActivity.java

 public class Demo_tabActivity extends Activity 
 {

private static String NAMESPACE = "http://tempuri.org/";
   private static String METHOD_NAME = "FahrenheitToCelsius";
   private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
   private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

   Button btnFar;
   EditText txtFar;

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       btnFar = (Button)findViewById(R.id.button1);

       txtFar = (EditText)findViewById(R.id.editText_in);

       btnFar.setOnClickListener(new View.OnClickListener()
       {
       public void onClick(View v)
       {
           String b;

         //Initialize soap request + add parameters
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

         //Use this to add parameters
         request.addProperty("Fahrenheit",txtFar.getText().toString());

         //Declare the version of the SOAP request
         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

         envelope.setOutputSoapObject(request);
         envelope.dotNet = true;

         try 
         {
             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

             //this is the actual part that will call the webservice
             androidHttpTransport.call(SOAP_ACTION, envelope);

             // Get the SoapResult from the envelope body.

             SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

             if(result != null)
             {
              //Get the first property and change the label text

                b = result.toString();
                Intent itnt = new Intent(v.getContext(), Tab_1.class);
                itnt.putExtra("gotonextpage", b.toString());
                startActivity(itnt);
             }
             else
             {
           Toast.makeText(getApplicationContext(), "NoResponse",Toast.LENGTH_SHORT).show();
             }
         }
        catch (Exception e)
        {
           e.printStackTrace();
           }
         }
       });
       }

注意:我只怀疑上面代码中的 if 条件


Tab_1.java

public class Tab_1 extends Activity 
{
TextView tv;
String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);

Bundle extras = getIntent().getExtras();
if(extras != null)
{   result = extras.getString("gotonextpage");  }
tv = (TextView)findViewById(R.id.textView_main2);
tv.setText(result);
}}

Tabhost.java

 public class Tabhost extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);


    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

        intent = new Intent().setClass(this, Tab_1.class);
    spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Tab_2.class);
    spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
    }
}

非常感谢!..

4

2 回答 2

3
EDIT:
Demo_tabActivity.java

 public class Demo_tabActivity extends Activity 
 {

private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "FahrenheitToCelsius";
private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?
WDL";

  Button btnFar;
EditText txtFar;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    btnFar = (Button)findViewById(R.id.button1);

    txtFar = (EditText)findViewById(R.id.editText_in);

   btnFar.setOnClickListener(new View.OnClickListener()
   {
   public void onClick(View v)
   {
       String b;

     //Initialize soap request + add parameters
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

     //Use this to add parameters
     request.addProperty("Fahrenheit",txtFar.getText().toString());

     //Declare the version of the SOAP request
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

     envelope.setOutputSoapObject(request);
     envelope.dotNet = true;

     try 
     {
         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

         //this is the actual part that will call the webservice
         androidHttpTransport.call(SOAP_ACTION, envelope);

         // Get the SoapResult from the envelope body.

         SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

         if(result != null)
         {
          //Get the first property and change the label text

            b = result.toString();
            Intent itnt = new Intent(v.getContext(), Tabhost.class);
            itnt.putExtra("gotonextpage", b.toString());
            startActivity(itnt);
         }
         else
         {
       Toast.makeText(getApplicationContext(), "NoResponse",Toast.LENGTH_SHORT).show();
         }
     }
    catch (Exception e)
    {
       e.printStackTrace();
       }
     }
   });
   }



 public class Tabhost extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
String result;
Bundle extras = getIntent().getExtras();
    if(extras != null){   
    result = extras.getString("gotonextpage");  
    }


TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;

    intent = new Intent().setClass(this, Tab_1.class);
spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
 intent.putExtra("gotonextpage", result);
        startActivity(itnt);
tabHost.addTab(spec);

intent = new Intent().setClass(this, Tab_2.class);
spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);
}
}


Tab_1.java

public class Tab_1 extends Activity 
{
TextView tv;
String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);

 Bundle extras = getIntent().getExtras();
if(extras != null)
{   result = extras.getString("gotonextpage");  }
tv = (TextView)findViewById(R.id.textView_main2);
tv.setText(result);
}}

我没有尝试,但很可能您需要在 Demo_tabActivity.java 类中运行 TabHost。因为如果您调用选项卡主机类,它将分别调用选项卡一和选项卡二活动。(取决于选项卡主机中哪个选项卡设置为当前选项卡)。尝试让我知道结果!

于 2012-06-25T06:27:51.353 回答
1

您需要使用 TabGroupActivity 来显示 TabHost 中的每个活动。在 TabGroupActivity 上查看此链接,看看它是如何工作的。这将在您启动的任何活动中显示您的选项卡。这应该可以解决您的问题。http://androidmaterial.blogspot.in/2011/04/how-to-us-tab-group-activity-in-android.html

于 2012-06-25T06:32:09.077 回答