android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算
本文實(shí)例為大家分享了android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算的具體代碼,供大家參考,具體內(nèi)容如下
主界面上是兩個(gè)EditText和一個(gè)按鈕。用于輸入兩個(gè)數(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="計(jì)算" ? ? ? ? /> ? </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);
?? ?}
?? ?
}
}計(jì)算結(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>
接收兩個(gè)數(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+"");
?? ??? ?
?? ?}
?
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android顯式Intent與隱式Intent的使用詳解
- Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問(wèn)題解決
- Android開發(fā)Intent跳轉(zhuǎn)傳遞list集合實(shí)現(xiàn)示例
- Android13?加強(qiáng)Intent?filters?的安全性
- Android使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- Android Intent傳遞數(shù)據(jù)大小限制詳解
- Android開發(fā)中Intent.Action各種常見的作用匯總
- Android使用Intent隱式實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的兩種方法
- Android Intent基礎(chǔ)用法及作用詳解
相關(guān)文章
Flutter實(shí)現(xiàn)旋轉(zhuǎn)掃描效果
這篇文章主要介紹了通過(guò)Flutter RotationTransition實(shí)現(xiàn)雷達(dá)旋轉(zhuǎn)掃描的效果,文中的示例代碼講解詳細(xì),感興趣的同學(xué)可以動(dòng)手試一試2022-01-01
android預(yù)置默認(rèn)的語(yǔ)音信箱號(hào)碼具體實(shí)現(xiàn)
在此介紹以xml的方式預(yù)置VM number的方法,以及如何允許用戶去修改并能夠記住用戶的選擇2013-06-06
Android開發(fā)RecyclerView性能優(yōu)化之異步預(yù)加載
這篇文章主要介紹了Android開發(fā)RecyclerView性能優(yōu)化之異步預(yù)加載實(shí)現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Android Studio 3.0被調(diào)方法參數(shù)名提示的取消方法
這篇文章主要介紹了去掉android studio 3.0被調(diào)方法參數(shù)名提示的解決方法,在文章末尾給大家補(bǔ)充介紹了Android Studio 3.0 gradle提示太老的解決方法,非常不錯(cuò),需要的朋友可以參考下2017-11-11
Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法,較為詳細(xì)的分析了Android中ImageView控件的scaleType屬性控制圖片縮放的具體用法,需要的朋友可以參考下2016-01-01
Android 軟鍵盤彈出時(shí)把原來(lái)布局頂上去的解決方法
本文主要介紹了Android軟鍵盤彈出時(shí)把原來(lái)布局頂上去的解決方法。具有一定的參考作用,下面跟著小編一起來(lái)看下吧2017-01-01
android上實(shí)現(xiàn)0.5px線條的原理分析
這篇文章主要介紹了android上實(shí)現(xiàn)0.5px線條的原理分析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Android中PathMeasure仿支付寶支付動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android中PathMeasure仿支付寶支付動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

