我正在做一个简单的Android应用程序(一个度量转换器),我正在使用两个活动:第一个活动是主应用程序,第二个活动用于显示结果。
当我输入值时,选择转换类型(例如码到厘米)并单击结果按钮,结果仅出现在第一个活动(而不是第二个活动)中。
这是我的代码:
ActivityMain.java
package displayinsecact.project;
import android.app.*;
import android.content.Intent;
import android.os.*;
import android.view.*;
import android.widget.*;
public class ActivityMain extends Activity{
private EditText etxt;
private TextView txtViewRes;
private CheckBox chkBoxYarCen, chkBoxCenYar, chkBoxYarMet, chkBoxMetYar, chkBoxYarPie, chkBoxPieYar;
private ConversorMedidas current;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialUISetup();
txtViewRes=(TextView)findViewById(R.id.txtViewRes);
}
public void initialUISetup(){
etxt=(EditText)findViewById(R.id.etxt);
txtViewRes=(TextView)findViewById(R.id.txtViewRes);
chkBoxYarCen=(CheckBox)findViewById(R.id.chkBoxYarCen);
chkBoxCenYar=(CheckBox)findViewById(R.id.chkBoxCenYar);
chkBoxYarMet=(CheckBox)findViewById(R.id.chkBoxYarMet);
chkBoxMetYar=(CheckBox)findViewById(R.id.chkBoxMetYar);
chkBoxYarPie=(CheckBox)findViewById(R.id.chkBoxYarPie);
chkBoxPieYar=(CheckBox)findViewById(R.id.chkBoxPieYar);
}
public void exchange(View v){
double aux=0;
if(chkBoxYarCen.isChecked()==true){
current=new convYarCen();
aux=current.yardasCentimetros(Double.parseDouble(etxt.getText().toString()));
}
if(chkBoxCenYar.isChecked()==true){
current=new convYarCen();
aux=current.yardasMetros(Double.parseDouble(etxt.getText().toString()));
}
if(chkBoxYarMet.isChecked()==true){
current=new convYarMet();
aux=current.yardasMetros(Double.parseDouble(etxt.getText().toString()));
}
if(chkBoxMetYar.isChecked()==true){
current=new convYarMet();
aux=current.yardasCentimetros(Double.parseDouble(etxt.getText().toString()));
}
if(chkBoxYarPie.isChecked()==true){
current=new convYarPie();
aux=current.yardasCentimetros(Double.parseDouble(etxt.getText().toString()));
}
if(chkBoxPieYar.isChecked()==true){
current=new convYarPie();
aux=current.yardasMetros(Double.parseDouble(etxt.getText().toString()));
}
txtViewRes.setText(String.valueOf(aux));
}
String contenido;
public void showThirdActivity(View v){
contenido=txtViewRes.getText().toString();
Intent i=new Intent(this, ResultadoActivity.class);
i.putExtra("resultado", contenido);
startActivity(i);
}
}
ResultActivity.java
package displayinsecact.project;
import android.app.Activity;
import android.os.*;
import android.widget.*;
public class ResultadoActivity extends Activity{
TextView tvDatosRecibidos;
String cadena;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.resultado);
tvDatosRecibidos=(TextView)findViewById(R.id.tvDatosRecibidos);
Bundle recogerDatos=getIntent().getExtras();
cadena=recogerDatos.getString("resultado");
tvDatosRecibidos.setText(cadena);
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the second activity" />
<TextView
android:id="@+id/txtViewData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input Data"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etxt"><requestFocus></requestFocus>
</EditText>
<!--
chkBoxYarCen, chkBoxCenYar, chkBoxYarMet, chkBoxMetYar, chkBoxYarPie, chkBoxPieYar;
-->
<CheckBox
android:id="@+id/chkBoxYarCen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yards to Centimetres"
/>
<CheckBox
android:id="@+id/chkBoxCenYar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centimetres to Yards"
/>
<CheckBox
android:id="@+id/chkBoxYarMet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yards to Metres"
/>
<CheckBox
android:id="@+id/chkBoxMetYar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foot to Yards"
/>
<CheckBox
android:id="@+id/chkBoxYarPie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yards to Foots"
/>
<CheckBox
android:id="@+id/chkBoxPieYar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foots to Yards"
/>
<!-- Botones -->
<Button
android:id="@+id/btnOpe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exchange"
android:onClick="exchange"
/>
<TextView
android:id="@+id/txtViewRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Resultado"
/>
resultado.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Datos recibidos de la actividad anterior"
/>
<TextView
android:id="@+id/tvDatosRecibidos"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="displayinsecact.project"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ActivityMain"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="Resultado Activity"
android:name=".Resultadoctivity">
<intent-filter >
<action android:name=".Resultadoctivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
很抱歉给您带来不便。
答案 0 :(得分:0)
在第一项活动中更改您的代码
Intent i=new Intent(this, ResultadoActivity.class);
Bundle b =new Bundle();
b.putString("resultado", contenido);
i.putExtras(b);
startActivity(i);