Android實(shí)戰(zhàn)教程第一篇之最簡(jiǎn)單的計(jì)算器
從今天開始,本專欄持續(xù)更新Android簡(jiǎn)易實(shí)戰(zhàn)類博客文章。和以往專欄不同,此專欄只有實(shí)例。每個(gè)實(shí)例盡量按照知識(shí)點(diǎn)對(duì)應(yīng)相應(yīng)一章節(jié)的內(nèi)容去寫,循序漸進(jìn)。有些實(shí)例可能會(huì)與另一個(gè)專欄有重復(fù)的文章。
開始本專欄的第一個(gè)簡(jiǎn)易案例:
首先設(shè)置兩個(gè)布局文件,一個(gè)布局文件進(jìn)行輸入數(shù)據(jù),獲取加法運(yùn)算;另一個(gè)布局文件進(jìn)行顯示最終結(jié)果。Activity1啟動(dòng)Activity2,并傳遞計(jì)算結(jié)果值給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" /> <Button android:id="@+id/calculate" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
頁面展示:
result.xml
activity03活動(dòng):
package mars.activity03; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; //1.在Activity03當(dāng)中,要聲明四個(gè)控件 //2.要為其中的兩個(gè)控件設(shè)置顯示的值 //3.創(chuàng)建一個(gè)監(jiān)聽器類,監(jiān)聽按鈕按下的動(dòng)作 //4.將監(jiān)聽器類的對(duì)象,綁定在按鈕對(duì)象上 public class Activity03 extends Activity { /** Called when the activity is first created. */ private EditText factorOne ; private EditText factorTwo; private TextView symbol; private Button calculate; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根據(jù)控件的ID來取得代表控件的對(duì)象 factorOne = (EditText)findViewById(R.id.factorOne); factorTwo = (EditText)findViewById(R.id.factorTwo); symbol = (TextView)findViewById(R.id.symbol); calculate = (Button)findViewById(R.id.calculate); //為symbol和calculate設(shè)置顯示的值 // symbol.setText("乘以"); // calculate.setText("計(jì)算"); symbol.setText(R.string.symbol);//這里通過引用的方式,去String文件中引用。保證了業(yè)務(wù)邏輯、視圖、引用資源分開 calculate.setText(R.string.calculate); //將監(jiān)聽器的對(duì)象綁定到按鈕對(duì)象上面 calculate.setOnClickListener(new CalculateListener()); } //當(dāng)客戶點(diǎn)擊MENU按鈕的時(shí)候,調(diào)用該方法 @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 1, R.string.exit); menu.add(0,2,2,R.string.about); return super.onCreateOptionsMenu(menu); } //當(dāng)客戶點(diǎn)擊菜單當(dāng)中的某一個(gè)選項(xiàng)時(shí),會(huì)調(diào)用該方法 @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == 1){ finish(); } return super.onOptionsItemSelected(item); } class CalculateListener implements OnClickListener{ @Override public void onClick(View v) { //取得兩個(gè)EditText控件的值 String factorOneStr = factorOne.getText().toString(); String factorTwoStr = factorTwo.getText().toString(); //將這兩個(gè)值存放到Intent對(duì)象當(dāng)中 Intent intent = new Intent(); intent.putExtra("one",factorOneStr); intent.putExtra("two",factorTwoStr); intent.setClass(Activity03.this, ResultActivity.class); //使用這個(gè)Intent對(duì)象來啟動(dòng)ResultActivity Activity03.this.startActivity(intent); } } }
resultActivity:
package mars.activity03; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; //1.接受從Activity03當(dāng)中所傳遞的值 //2.計(jì)算兩個(gè)值的積 //3.將計(jì)算的結(jié)果顯示在Activity上 public class ResultActivity extends Activity{ private TextView resultView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.result); resultView = (TextView)findViewById(R.id.result); //得到Intent對(duì)象當(dāng)中的值 Intent intent = getIntent(); String factorOneStr = intent.getStringExtra("one"); String factorTwoStr = intent.getStringExtra("two"); int factorOneInt = Integer.parseInt(factorOneStr); int factorTwoInt = Integer.parseInt(factorTwoStr); //計(jì)算兩個(gè)值的積 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">計(jì)算</string> <string name="exit">退出</string> <string name="about">關(guān)于</string> </resources>
最后再看一下配置文件:活動(dòng)都要進(jìn)行注冊(cè),并且設(shè)置Activity03為主活動(dòng)
<?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標(biāo)題欄顯示result--> </application> <uses-sdk android:minSdkVersion="4" /> </manifest>
結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能【附完整demo源碼下載】
- android計(jì)算器簡(jiǎn)單實(shí)現(xiàn)代碼
- 簡(jiǎn)單實(shí)現(xiàn)Android計(jì)算器功能
- Android計(jì)算器簡(jiǎn)單邏輯實(shí)現(xiàn)實(shí)例分享
- Android開發(fā)學(xué)習(xí)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- Android開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- Android實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
- Android簡(jiǎn)單實(shí)現(xiàn)計(jì)算器功能
- Android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
相關(guān)文章
Android自定義控件單位尺寸實(shí)現(xiàn)代碼
這篇文章主要介紹了Android自定義控件單位尺寸實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Flutter本地存儲(chǔ)之基本的鍵值對(duì)存儲(chǔ)詳解
在原生的?Android?或?iOS?中,都提供了基本的鍵值對(duì)存儲(chǔ)方式,在?Flutter?中,提供了?shared_preferences?這個(gè)插件來實(shí)現(xiàn)本地鍵值對(duì)數(shù)據(jù)存儲(chǔ),本文就來和大家簡(jiǎn)單聊聊吧2023-03-03Android 反射注解與動(dòng)態(tài)代理綜合使用詳解
本篇文章主要介紹了Android 反射注解與動(dòng)態(tài)代理綜合使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Android實(shí)現(xiàn)字幕滾動(dòng)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)字幕滾動(dòng)的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07朋友圈實(shí)現(xiàn)圖片+文字轉(zhuǎn)發(fā)功能(必看篇)
下面小編就為大家?guī)硪黄笥讶?shí)現(xiàn)圖片+文字轉(zhuǎn)發(fā)功能(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03Android手勢(shì)ImageView三部曲 第三部
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)ImageView三部曲的第三部,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03