在androideclipse上处理tabhost

在androideclipse上处理tabhost,android,eclipse,android-tabhost,Android,Eclipse,Android Tabhost,我有四个活动, Demo_tabActivity.java[主活动] Tabhost.java 以下两个活动是上述tabhost.java的选项卡 Tab_1.java Tab_2.java 第一个activityDemo_tabActivity.java包含一个edittext&按钮。第二个Tabhost.java活动包含一个Tabhost小部件。第三个和第四个活动分别包含TextView 第一个活动将通过从用户获取输入来使用web服务,并返回关于tabhostsecond活动的第一个tab第

我有四个活动,

Demo_tabActivity.java[主活动]

Tabhost.java

以下两个活动是上述tabhost.java的选项卡

Tab_1.java

Tab_2.java

第一个activityDemo_tabActivity.java包含一个edittext&按钮。第二个Tabhost.java活动包含一个Tabhost小部件。第三个和第四个活动分别包含TextView

第一个活动将通过从用户获取输入来使用web服务,并返回关于tabhostsecond活动的第一个tab第三个活动的一些数据

Web服务运行良好,并能完美地返回值

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

Demo_tabActivity.java

注:我只对上述代码中的if条件表示怀疑

Tab_1.java

Tabhost.java

非常感谢

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。因为如果调用tab主机类,它将分别调用tab 1和tab 2活动。取决于在选项卡主机中将哪个选项卡设置为当前选项卡。试着让我知道结果



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

您需要使用TabGroupActivity来显示TabHost中的每个活动。在TabGroupActivity上查看此链接并了解其工作原理。这将在您启动的任何活动中显示您的选项卡。这将解决您的问题

您需要使用TabGroupActivity来显示TabHost中的每个活动。在TabGroupActivity上查看此链接并了解其工作原理。这将在您启动的任何活动中显示您的选项卡。这将解决您的问题

osayilgan,如果我放置Tabhost.class而不是Tab_1.java,它只会显示没有答案的Tabhost小部件。因为您调用的是Tab_1.java,其结果是您在第一次活动中得到的结果。或者我有一个更好的方法,您可以调用您的Tab主机,其中包含您在第一次活动中放入Tab_1.java的额外内容,然后在tab主机中获取额外内容,然后通过获取并放入这些额外数据调用tab_1.java。将额外内容放入tab主机,并将这些额外内容放入您调用tab_1.java的意图中。之后,您将在Tab_1活动中获得这些额外数据。只是再长一点,你已经做到了。但是,在你放入额外内容之前,别忘了将其转换为字符串,我想我没有编写它。Demo_tabActivity.java包含主要概念,那么我如何才能将额外内容放入tabhost.java?osayilgan,如果我将tabhost.class而不是Tab_1.java,它只显示没有答案的tabhost小部件。你试图获取的数据,一定要尝试进入Tab_1.java。因为您调用的是Tab_1.java,其结果是您在第一次活动中得到的结果。或者我有一个更好的方法,您可以调用您的Tab主机,其中包含您在第一次活动中放入Tab_1.java的额外内容,然后在tab主机中获取额外内容,然后通过获取并放入这些额外数据调用tab_1.java。将额外内容放入tab主机,并将这些额外内容放入您调用tab_1.java的意图中。之后,您将在Tab_1活动中获得这些额外数据。只是再长一点,你已经做到了。但是在你放入额外内容之前,别忘了将其转换为字符串,我想我没有编写它。Demo_tabActivity.java包含主要概念,那么我如何才能将额外内容放入tabhost.java?这应该可以完成你的工作,我遇到了一个类似的问题,并使用它实现了,它运行得非常好。您能告诉我我的项目有哪些地方需要改进吗…我做错了什么。将TabGroupActivity类复制到您的项目中,而不是扩展Activity扩展Tab_1和Tab_2的TabGroupActivity类。然后在每个选项卡中使用startChildActivity,这将使您的选项卡可见。在提供的链接中明确提到了这一点。这应该可以让你完成工作,我遇到了一个类似的问题,并使用它实现了,它运行得非常好。您能告诉我我的项目有哪些地方需要改进吗…我做错了什么。将TabGroupActivity类复制到您的项目中,而不是扩展Activity扩展Tab_1和Tab_2的TabGroupActivity类。然后在每个选项卡中使用startChildActivity,这将使您的选项卡可见。在提供的链接中明确提到了这一点。
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);
}}
 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);
    }
}
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);
}}