Android初级教程Activity小案例(计算器乘法运算)
生活随笔
收集整理的這篇文章主要介紹了
Android初级教程Activity小案例(计算器乘法运算)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先設置兩個布局文件,一個布局文件進行輸入數據,獲取加法運算;另一個布局文件進行顯示最終結果。Activity1啟動Activity2,并傳遞計算結果值給Activity2.
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"> <EditText android:id="@+id/factorOne"android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/symbol"android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/factorTwo"android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Buttonandroid:id="@+id/calculate"android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>頁面展示:
result.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/result"android:layout_width="fill_parent"android:layout_height="wrap_content"/> </LinearLayout>界面展示:
activity03活動:
resultActivity: package mars.activity03;import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; //1.接受從Activity03當中所傳遞的值 //2.計算兩個值的積 //3.將計算的結果顯示在Activity上 public class ResultActivity extends Activity{private TextView resultView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.result);resultView = (TextView)findViewById(R.id.result);//得到Intent對象當中的值 Intent intent = getIntent();String factorOneStr = intent.getStringExtra("one");String factorTwoStr = intent.getStringExtra("two");int factorOneInt = Integer.parseInt(factorOneStr);int factorTwoInt = Integer.parseInt(factorTwoStr);//計算兩個值的積int result = factorOneInt * factorTwoInt;resultView.setText(result + "");}}
String.xml: <?xml version="1.0" encoding="utf-8"?> <resources><string name="hello">Hello World, Activity03!</string><string name="app_name">activity03</string><string name="resultLabel">result</string><string name="symbol">乘以</string><string name="calculate">計算</string><string name="exit">退出</string><string name="about">關于</string> </resources>
最后再看一下配置文件:活動都要進行注冊,并且設置Activity03為主活動
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="mars.activity03"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".Activity03"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".ResultActivity" android:label="@string/resultLabel"/><!--這里使ResultActivity標題欄顯示result--></application><uses-sdk android:minSdkVersion="4" /></manifest>
運行結果:
轉載于:https://www.cnblogs.com/wanghang/p/6299687.html
總結
以上是生活随笔為你收集整理的Android初级教程Activity小案例(计算器乘法运算)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 完成了C++作业,本博客现在开始全面记录
- 下一篇: 第14周个人进度条