日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

android使用intent傳遞參數實現乘法計算_Android

作者:被遺忘的秋天 ? 更新時間: 2022-06-22 編程語言

本文實例為大家分享了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+"");
?? ??? ?
?? ?}
?
}

原文鏈接:https://blog.csdn.net/zhu_hua_jie/article/details/10071667

欄目分類
最近更新