android 使用intent传递参数实现乘法计算
2013-08-19 15:10
686 查看
主界面上是两个EditText和一个按钮。用于输入两个数字参数。
calcute.xml:
处理calcute的java程序
CaluteMain.java:
计算结果的界面:caluteresult.xml:
接收两个数字参数并显示结果的Activity。CaluteResult.java:
calcute.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="wrap_content" android:orientation="vertical" android:gravity="center"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > <EditText android:id="@+id/factory1" android:layout_height="wrap_content" android:layout_width="100dip" /> <TextView android:layout_width="50dip" android:layout_height="wrap_content" android:text="X" android:layout_marginLeft="30dip" /> <EditText android:id="@+id/factory2" android:layout_height="wrap_content" android:layout_width="100dip" /> </LinearLayout> <Button android:id="@+id/calute" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算" /> </LinearLayout>
处理calcute的java程序
CaluteMain.java:
package com.example.wenandroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class CaluteMain extends Activity { private EditText factory1; private EditText factory2; private Button calute; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.calcute); factory1=(EditText)findViewById(R.id.factory1); factory2=(EditText)findViewById(R.id.factory2); calute=(Button)findViewById(R.id.calute); calute.setOnClickListener(new MyOnClickListener()); } class MyOnClickListener implements OnClickListener{ @Override public void onClick(View v) { String factoryStr1=factory1.getText().toString(); String factoryStr2=factory2.getText().toString(); Intent intent=new Intent(CaluteMain.this,CaluteResult.class); intent.putExtra("one", factoryStr1); intent.putExtra("two", factoryStr2); startActivity(intent); } } }
计算结果的界面:caluteresult.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
接收两个数字参数并显示结果的Activity。CaluteResult.java:
package com.example.wenandroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class CaluteResult extends Activity { private TextView resultView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.caluteresult); resultView=(TextView)findViewById(R.id.result); Intent intent=getIntent(); String factoryStr1=intent.getStringExtra("one"); String factoryStr2=intent.getStringExtra("two"); //将字符串转换为整形 int factoryInt1=Integer.parseInt(factoryStr1); int factoryInt2=Integer.parseInt(factoryStr2); int result=factoryInt1*factoryInt2; resultView.setText("结果是:"+result+""); } }
相关文章推荐
- android 使用intent传递参数实现乘法计算
- Android Activity中使用Intent实现页面跳转与参数传递的方法
- Android 使用intent.putExtra实现Activity之间的参数传递
- Android 使用intent.putExtra实现Activity之间的参数传递
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- 使用Intent对象来实现两个(或多个)Activity之间的跳转,并实现在Activity之间传递参数。
- Android使用Intent传递复杂参数及复杂参数列表
- Activity中使用Intent实现页面跳转与参数的传递
- Android编程实现全局获取Context及使用Intent传递对象的方法详解
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- Android使用Intent传递复杂参数及复杂参数列表
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递
- Activity中使用Intent实现页面跳转与参数的传递(转)
- android PendingIntent 使用通知传递多个参数,及不覆盖的问题
- Android 使用Intent传递数据的实现思路与代码
- Android,使用Intent或Bundle传递参数,跳转页面。
- Android Bundle的使用及与Intent传递参数区别
- Android Bundle的使用及与Intent传递参数区别
- 【Android】使用Intent实现数据传递
- Android,使用Intent或Bundle传递参数,跳转页面