android使用intent傳遞參數(shù)實現(xiàn)乘法計算
本文實例為大家分享了android使用intent傳遞參數(shù)實現(xiàn)乘法計算的具體代碼,供大家參考,具體內(nèi)容如下
主界面上是兩個EditText和一個按鈕。用于輸入兩個數(shù)字參數(shù)。
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);
?? ?}
?? ?
}
}計算結(jié)果的界面: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>
接收兩個數(shù)字參數(shù)并顯示結(jié)果的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");
?? ??? ?//將字符串轉(zhuǎn)換為整形
?? ??? ?int factoryInt1=Integer.parseInt(factoryStr1);
?? ??? ?int factoryInt2=Integer.parseInt(factoryStr2);
?? ??? ?int result=factoryInt1*factoryInt2;
?? ??? ?resultView.setText("結(jié)果是:"+result+"");
?? ??? ?
?? ?}
?
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程實現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進(jìn)行解析的方法
這篇文章主要介紹了Android編程實現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進(jìn)行解析的方法,結(jié)合實例形式分析了Android的經(jīng)緯度地址解析與json格式數(shù)據(jù)操作相關(guān)技巧,需要的朋友可以參考下2017-02-02
Android本地存儲SharedPreferences詳解
這篇文章主要介紹了Android本地存儲SharedPreferences詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
WebView設(shè)置WebViewClient的方法
這篇文章主要介紹了 WebView設(shè)置WebViewClient的方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
詳解LeakCanary分析內(nèi)存泄露如何實現(xiàn)
這篇文章主要為大家介紹了詳解LeakCanary分析內(nèi)存泄露如何實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android Native 內(nèi)存泄漏系統(tǒng)化解決方案
這篇文章主要介紹了Android Native 內(nèi)存泄漏系統(tǒng)化解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android實現(xiàn)圖片自動切換功能(實例代碼詳解)
這篇文章主要介紹了Android實現(xiàn)圖片自動切換功能,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

