gpt4 book ai didi

android - 在 android eclipse 上处理 tabhost

转载 作者:行者123 更新时间:2023-11-29 16:13:36 25 4
gpt4 key购买 nike

我有四个 Activity ,即

  • Demo_tabActivity.java [主要 Activity ]

  • Tabhost.java

下面两个activity是上面tabhost.java的tab

  • Tab_1.java

  • Tab_2.java

第一个 Activity (Demo_tabActivity.java) 包含一个编辑文本和按钮。第二个 (Tabhost.java) Activity 包含一个 Tabhost 小部件。第三个和第四个 Activity 包含分别是 TextView 。

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

使用 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);
}
}

非常感谢!..

最佳答案

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。因为如果您调用选项卡主机类,它将分别调用选项卡一和选项卡二 Activity 。 (取决于哪个选项卡设置为选项卡主机中的当前选项卡)。尝试并让我知道结果!

关于android - 在 android eclipse 上处理 tabhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11184288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com