Android實(shí)戰(zhàn)教程第一篇之最簡單的計算器
從今天開始,本專欄持續(xù)更新Android簡易實(shí)戰(zhàn)類博客文章。和以往專欄不同,此專欄只有實(shí)例。每個實(shí)例盡量按照知識點(diǎn)對應(yīng)相應(yīng)一章節(jié)的內(nèi)容去寫,循序漸進(jìn)。有些實(shí)例可能會與另一個專欄有重復(fù)的文章。
開始本專欄的第一個簡易案例:
首先設(shè)置兩個布局文件,一個布局文件進(jìn)行輸入數(shù)據(jù),獲取加法運(yùn)算;另一個布局文件進(jìn)行顯示最終結(jié)果。Activity1啟動Activity2,并傳遞計算結(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活動:
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)中,要聲明四個控件
//2.要為其中的兩個控件設(shè)置顯示的值
//3.創(chuàng)建一個監(jiān)聽器類,監(jiān)聽按鈕按下的動作
//4.將監(jiān)聽器類的對象,綁定在按鈕對象上
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來取得代表控件的對象
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("計算");
symbol.setText(R.string.symbol);//這里通過引用的方式,去String文件中引用。保證了業(yè)務(wù)邏輯、視圖、引用資源分開
calculate.setText(R.string.calculate);
//將監(jiān)聽器的對象綁定到按鈕對象上面
calculate.setOnClickListener(new CalculateListener());
}
//當(dāng)客戶點(diǎn)擊MENU按鈕的時候,調(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)中的某一個選項(xiàng)時,會調(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) {
//取得兩個EditText控件的值
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
//將這兩個值存放到Intent對象當(dāng)中
Intent intent = new Intent();
intent.putExtra("one",factorOneStr);
intent.putExtra("two",factorTwoStr);
intent.setClass(Activity03.this, ResultActivity.class);
//使用這個Intent對象來啟動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.計算兩個值的積
//3.將計算的結(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對象當(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);
//計算兩個值的積
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">關(guān)于</string> </resources>
最后再看一下配置文件:活動都要進(jìn)行注冊,并且設(shè)置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標(biāo)題欄顯示result--> </application> <uses-sdk android:minSdkVersion="4" /> </manifest>
結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)實(shí)現(xiàn)的簡單計算器功能【附完整demo源碼下載】
- android計算器簡單實(shí)現(xiàn)代碼
- 簡單實(shí)現(xiàn)Android計算器功能
- Android計算器簡單邏輯實(shí)現(xiàn)實(shí)例分享
- Android開發(fā)學(xué)習(xí)實(shí)現(xiàn)簡單計算器
- Android開發(fā)實(shí)現(xiàn)簡單計算器功能
- Android實(shí)現(xiàn)簡單加法計算器
- Android簡單實(shí)現(xiàn)計算器功能
- Android實(shí)現(xiàn)簡單計算器
- Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個簡單計算器
相關(guān)文章
Android自定義控件單位尺寸實(shí)現(xiàn)代碼
這篇文章主要介紹了Android自定義控件單位尺寸實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
朋友圈實(shí)現(xiàn)圖片+文字轉(zhuǎn)發(fā)功能(必看篇)
下面小編就為大家?guī)硪黄笥讶?shí)現(xiàn)圖片+文字轉(zhuǎn)發(fā)功能(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03

