【问题标题】:Handling tabhost on android eclipse在 android eclipse 上处理 tabhost
【发布时间】:2012-06-25 06:19:53
【问题描述】:

我有四个活动,即,

  • 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);
    }
}

非常感谢!..

【问题讨论】:

    标签: android eclipse android-tabhost


    【解决方案1】:
    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。因为如果您调用选项卡主机类,它将分别调用选项卡一和选项卡二活动。 (取决于选项卡主机中哪个选项卡设置为当前选项卡)。试着告诉我结果!

    【讨论】:

    • osayilgan,如果我放置 Tabhost.class 而不是 Tab_1.java 它只会显示没有答案的 tabhost 小部件。
    • 你尝试获取什么数据,请尝试在 Tab_1.java 中获取。因为您正在使用您在第一个活动中获得的结果调用 Tab_1.java。
    • 或者我有一个更好的方法,您可以使用您从第一个活动中放置 Tab_1.java 的额外内容调用您的标签主机,然后在标签主机中获取额外的内容,然后调用 Tab_1。 java 通过获取然后放置这些额外的数据。
    • 将额外的内容放到标签宿主中,并获取这些额外的内容以放入您调用 Tab_1.java 的意图中。之后,您将在 Tab_1 活动中获得这些额外数据。只是有点长的路,你已经这样做了。不过在放多余的内容之前别忘了转成字符串,我猜不是我写的。
    • Demo_tabActivity.java 包含了主要的概念,那么如何将多余的内容放到 tabhost.java 中呢?
    【解决方案2】:

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

    【讨论】:

    • 这应该可以完成您的工作,我遇到了类似的问题并使用它实现了它并且效果很好
    • 你能告诉我我需要对我的项目进行哪些修改吗...我在那里做错了什么。
    • 将 TabGroupActivity 类复制到您的项目中,而不是扩展 Activity,而是为 Tab_1 和 Tab_2 扩展 TabGroupActivity 类。然后在每个选项卡中使用 startChildActivity(),这将使您的选项卡可见。提供的链接中明确提及。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多