首先设置两个布局文件,一个布局文件进行输入数据,获取加法运算;另一个布局文件进行显示最终结果。Activity1启动Activity2,并传递计算结果值给Activity2.
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/factorOne" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/symbol" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/factorTwo" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/calculate" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
result.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
activity03活动:
package mars.activity03; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; //1.在Activity03当中,要声明四个控件 //2.要为其中的两个控件设置显示的值 //3.创建一个监听器类,监听按钮按下的动作 //4.将监听器类的对象,绑定在按钮对象上 public class Activity03 extends Activity { /** Called when the activity is first created. */ private EditText factorOne ; private EditText factorTwo; private TextView symbol; private Button calculate; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根据控件的ID来取得代表控件的对象 factorOne = (EditText)findViewById(R.id.factorOne); factorTwo = (EditText)findViewById(R.id.factorTwo); symbol = (TextView)findViewById(R.id.symbol); calculate = (Button)findViewById(R.id.calculate); //为symbol和calculate设置显示的值 // symbol.setText("乘以"); // calculate.setText("计算"); symbol.setText(R.string.symbol);//这里通过引用的方式,去String文件中引用。保证了业务逻辑、视图、引用资源分开 calculate.setText(R.string.calculate); //将监听器的对象绑定到按钮对象上面 calculate.setOnClickListener(new CalculateListener()); } //当客户点击MENU按钮的时候,调用该方法 @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 1, R.string.exit); menu.add(0,2,2,R.string.about); return super.onCreateOptionsMenu(menu); } //当客户点击菜单当中的某一个选项时,会调用该方法 @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == 1){ finish(); } return super.onOptionsItemSelected(item); } class CalculateListener implements OnClickListener{ @Override public void onClick(View v) { //取得两个EditText控件的值 String factorOneStr = factorOne.getText().toString(); String factorTwoStr = factorTwo.getText().toString(); //将这两个值存放到Intent对象当中 Intent intent = new Intent(); intent.putExtra("one",factorOneStr); intent.putExtra("two",factorTwoStr); intent.setClass(Activity03.this, ResultActivity.class); //使用这个Intent对象来启动ResultActivity Activity03.this.startActivity(intent); } } }
package mars.activity03; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; //1.接受从Activity03当中所传递的值 //2.计算两个值的积 //3.将计算的结果显示在Activity上 public class ResultActivity extends Activity{ private TextView resultView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.result); resultView = (TextView)findViewById(R.id.result); //得到Intent对象当中的值 Intent intent = getIntent(); String factorOneStr = intent.getStringExtra("one"); String factorTwoStr = intent.getStringExtra("two"); int factorOneInt = Integer.parseInt(factorOneStr); int factorTwoInt = Integer.parseInt(factorTwoStr); //计算两个值的积 int result = factorOneInt * factorTwoInt; resultView.setText(result + ""); } }
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Activity03!</string> <string name="app_name">activity03</string> <string name="resultLabel">result</string> <string name="symbol">乘以</string> <string name="calculate">计算</string> <string name="exit">退出</string> <string name="about">关于</string> </resources>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mars.activity03" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Activity03" 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:name=".ResultActivity" android:label="@string/resultLabel"/><!--这里使ResultActivity标题栏显示result--> </application> <uses-sdk android:minSdkVersion="4" /> </manifest>