本文实例为大家分享了android使用intent传递参数实现乘法计算的具体代码,供大家参考,具体内容如下
主界面上是两个EditText和一个按钮。用于输入两个数字参数。
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+""); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。