android實(shí)現(xiàn)簡(jiǎn)單的乘法計(jì)算代碼
開發(fā)環(huán)境:android4.1.
實(shí)驗(yàn)功能:
在第一個(gè)界面中的2個(gè)乘數(shù)輸入處分別輸入2個(gè)數(shù)字,按下結(jié)果button,會(huì)自動(dòng)跳到第二個(gè)界面并顯示輸入2個(gè)數(shù)字相乘的結(jié)果。如果在第一個(gè)界面中點(diǎn)擊系統(tǒng)的menu按鈕,則會(huì)自動(dòng)彈出一個(gè)菜單,菜單欄包括退出和關(guān)于2個(gè)按鈕,點(diǎn)擊退出按鈕,則退出了該程序。
實(shí)驗(yàn)說(shuō)明:
1. 第1個(gè)activity的步驟大概如下:
創(chuàng)建一個(gè)監(jiān)聽(tīng)器。
創(chuàng)建一個(gè)intent,并將獲取到的2個(gè)輸入數(shù)字分別以鍵值對(duì)的形式輸入intent。
連接第1個(gè)activity和第2個(gè)activity。
啟動(dòng)intent。
把該監(jiān)聽(tīng)器綁定到計(jì)算結(jié)果button中。
2.第2個(gè)activity的步驟大概如下:
創(chuàng)建一個(gè)監(jiān)聽(tīng)器。
創(chuàng)建一個(gè)intent,并從intent中的鍵值對(duì)獲得字符串,保存好。
將字符串轉(zhuǎn)換成數(shù)字,并相乘。
將相乘結(jié)果在TextView中顯示出來(lái)。
3. 加入菜單項(xiàng)需要重寫onCreateOptionsMenu()函數(shù),無(wú)需在xml布局中添加相應(yīng)的控件。
4. 相應(yīng)菜單單擊時(shí)需要重寫onOptionsItemSelected()函數(shù)。
5. 在android的java文件中導(dǎo)入包的時(shí)候,千萬(wàn)要注意區(qū)分名字的大小寫。
6. 一般在java程序中顯示文本時(shí),為了達(dá)到程序的國(guó)際化,最好不要把文本內(nèi)容在java中指定,而需要在對(duì)應(yīng)的xml文件中指定,然后在java代碼中調(diào)用其資源即可.因?yàn)檫@樣我們只需更改一個(gè)xml文件(當(dāng)然一般是有多個(gè)語(yǔ)言版本的xml文件,然后不同版本的軟件調(diào)用不同的xml即可)。
7. 內(nèi)部類在java中比較常見(jiàn),就是類中嵌入類,這種情況在多線程中應(yīng)用得比較多。在內(nèi)部類中可以使用外部類的成員變量,函數(shù)和對(duì)象等。
8. android控件中的menu控件不需要在xml中給出,直接在java源碼中重寫一些函數(shù)即可。
9. intent不僅可以在同一程序中的2個(gè)actibity中進(jìn)行傳遞數(shù)據(jù),還可以啊不同程序中的activity中進(jìn)行數(shù)據(jù)傳遞。
10. java中將字符串轉(zhuǎn)換成整數(shù)的語(yǔ)法是用Integer.parseInt(string);
11.java運(yùn)算中只要2者其中有一個(gè)為字符型,則會(huì)自動(dòng)將另一個(gè)也轉(zhuǎn)換成字符串 .
實(shí)驗(yàn)結(jié)果:
實(shí)驗(yàn)主要部分代碼及注釋:
MainActivity.java:
package com.example.factor;
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;
public class MainActivity extends Activity {
private TextView factor;
private EditText factor1;
private EditText factor2;
private Button result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
factor = (TextView)findViewById(R.id.factor);
factor1 = (EditText)findViewById(R.id.factor1);
factor2 = (EditText)findViewById(R.id.factor2);
result = (Button)findViewById(R.id.result);
factor.setText(R.string.factor);
result.setText(R.string.result);
//綁定一個(gè)監(jiān)聽(tīng)器到該按鈕
result.setOnClickListener(new onResultClickListener());
}
//該函數(shù)其實(shí)是一個(gè)回調(diào)函數(shù),但在系統(tǒng)中按下了menu按鈕時(shí)會(huì)自動(dòng)調(diào)用這個(gè)函數(shù)
@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);
}
//菜單項(xiàng)選擇函數(shù)重寫
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if( 1 == item.getItemId() )
finish();
return super.onOptionsItemSelected(item);
}
//創(chuàng)建一個(gè)監(jiān)聽(tīng)器
class onResultClickListener implements OnClickListener{
public void onClick(View v) {
//獲取2個(gè)edit輸入的數(shù)字
String factor1_str = factor1.getText().toString();
String factor2_str = factor2.getText().toString();
//建立intent,并將數(shù)據(jù)傳入,且激活ResultActivity
Intent intent = new Intent();
intent.putExtra("factor1", factor1_str);
intent.putExtra("factor2", factor2_str);
intent.setClass(MainActivity.this, ResultActivity.class);
//啟動(dòng)intent
MainActivity.this.startActivity(intent);
}
}
}
ResultActivity.java:
package com.example.factor;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class ResultActivity extends Activity {
private TextView result_dis;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
result_dis = (TextView)findViewById(R.id.result_view);
Intent intent = getIntent();
String factor_str1 = intent.getStringExtra("factor1");
String factor_str2 = intent.getStringExtra("factor2");
//java中將字符串轉(zhuǎn)換成整數(shù)的語(yǔ)法
int factor_int1 = Integer.parseInt(factor_str1);
int factor_int2 = Integer.parseInt(factor_str2);
int result = factor_int1 * factor_int2;
//java運(yùn)算中只要2者其中有一個(gè)為字符型,則會(huì)自動(dòng)將另一個(gè)也轉(zhuǎn)換成字符串
result_dis.setText(result + "");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_result, menu);
return true;
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/factor1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/factor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/factor2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
activity_result.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/result_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
></TextView>
</RelativeLayout>
實(shí)驗(yàn)總結(jié):
本次實(shí)驗(yàn)對(duì)intent的應(yīng)用有了稍許的了解,體會(huì)到了java和c++編程稍許不同的地方。
作者:tornadomeet
相關(guān)文章
Android中Gallery和ImageSwitcher的使用實(shí)例
今天小編就為大家分享一篇關(guān)于Android中Gallery和ImageSwitcher的使用實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Android Studio3.5開發(fā)工具(安卓開發(fā)工具)安裝步驟詳解
這篇文章主要為大家詳細(xì)介紹了Android Studio3.5開發(fā)工具安裝、安卓開發(fā)工具的安裝步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android源代碼倉(cāng)庫(kù)及其管理工具Repo分析詳解
本篇文章主要介紹了Android源代碼倉(cāng)庫(kù)及其管理工具Repo分析詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01android圖像繪制(四)自定義一個(gè)SurfaceView控件
自定義控件(類似按鈕等)的使用,自定義一個(gè)SurfaceView。如某一塊的動(dòng)態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下2013-01-01Android自定義View實(shí)現(xiàn)豎直跑馬燈效果案例解析
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)豎直跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android 逐幀動(dòng)畫創(chuàng)建實(shí)例詳解
這篇文章主要介紹了Android 逐幀動(dòng)畫創(chuàng)建實(shí)例詳解的相關(guān)資料,這里主要說(shuō)明Android 動(dòng)畫的創(chuàng)建及使用方法,希望通過(guò)此文能幫助到大家,需要的朋友可以參考下2017-08-08