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í)有所幫助,也希望大家多多支持腳本之家。
- Android顯式Intent與隱式Intent的使用詳解
- Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問題解決
- Android開發(fā)Intent跳轉(zhuǎn)傳遞list集合實現(xiàn)示例
- Android13?加強(qiáng)Intent?filters?的安全性
- Android使用Intent的Action和Data屬性實現(xiàn)點擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- Android Intent傳遞數(shù)據(jù)大小限制詳解
- Android開發(fā)中Intent.Action各種常見的作用匯總
- Android使用Intent隱式實現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android Intent基礎(chǔ)用法及作用詳解
相關(guān)文章
android預(yù)置默認(rèn)的語音信箱號碼具體實現(xiàn)
在此介紹以xml的方式預(yù)置VM number的方法,以及如何允許用戶去修改并能夠記住用戶的選擇2013-06-06
Android開發(fā)RecyclerView性能優(yōu)化之異步預(yù)加載
這篇文章主要介紹了Android開發(fā)RecyclerView性能優(yōu)化之異步預(yù)加載實現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Android Studio 3.0被調(diào)方法參數(shù)名提示的取消方法
這篇文章主要介紹了去掉android studio 3.0被調(diào)方法參數(shù)名提示的解決方法,在文章末尾給大家補充介紹了Android Studio 3.0 gradle提示太老的解決方法,非常不錯,需要的朋友可以參考下2017-11-11
Android開發(fā)之imageView圖片按比例縮放的實現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之imageView圖片按比例縮放的實現(xiàn)方法,較為詳細(xì)的分析了Android中ImageView控件的scaleType屬性控制圖片縮放的具體用法,需要的朋友可以參考下2016-01-01

