??Android Studio 專欄正在持續(xù)更新中,案例的原理圖解析、各種模塊分析??這里都有哦,同時(shí)也歡迎大家訂閱專欄,獲取更多詳細(xì)信息哦???
?個(gè)人主頁:零小唬的博客主頁
??歡迎大家 ??點(diǎn)贊 ??評論 ??收藏
?作者簡介:20級計(jì)算機(jī)專業(yè)學(xué)生一枚,來自寧夏,想從事前端開發(fā),目前還在努力學(xué)習(xí)并記錄博客中??
??本系列專欄: Android Studio
??希望本文對你在學(xué)習(xí)Android Studio的過程中有所幫助,如有不足請指正一起學(xué)習(xí),一起進(jìn)步??
?座右銘:只要你還愿意努力,世界一定會(huì)給你驚喜
實(shí)驗(yàn)說明
制作一個(gè)單位轉(zhuǎn)化app要求以及功能如下所述:
- 使用imageview插入一幅圖.png格式的圖片(附件中下載)
- 輸入框的顏色需要與上邊欄一致。(需要一個(gè)EditView 一個(gè)Button 以及一個(gè)TextView)
- 用戶輸入轉(zhuǎn)換的單位之后,點(diǎn)擊“COVERT”轉(zhuǎn)換按鈕后,結(jié)果將在TextView上顯示,如右圖所示。
![]()
- 附加要求(給追求挑戰(zhàn)的學(xué)生)多添加一個(gè)圖片,當(dāng)用戶點(diǎn)擊此圖片的時(shí)候,程序的背景顏色就會(huì)隨機(jī)發(fā)生變化。(附加要求完成,將給予實(shí)驗(yàn)分?jǐn)?shù)適當(dāng)獎(jiǎng)勵(lì)?。?br>
![]()
?????注:本實(shí)驗(yàn)來自帥帥的作業(yè)實(shí)驗(yàn)
實(shí)驗(yàn)分析
??要實(shí)現(xiàn)米和英尺轉(zhuǎn)換器的Android移動(dòng)應(yīng)用,可以采用Android Studio進(jìn)行設(shè)計(jì)和開發(fā)。以下是實(shí)現(xiàn)的大致步驟:
創(chuàng)建一個(gè)Android應(yīng)用程序項(xiàng)目,設(shè)置主界面布局和組件。
添加輸入框,用于用戶輸入數(shù)值米的值。
添加按鈕,用于觸發(fā)轉(zhuǎn)換操作。
編寫Java代碼,實(shí)現(xiàn)數(shù)值轉(zhuǎn)換算法??梢愿鶕?jù)用戶選擇的轉(zhuǎn)換單位,將輸入的數(shù)值乘以相應(yīng)的換算系數(shù),獲得轉(zhuǎn)換結(jié)果。
在Java代碼中,添加事件處理程序,以實(shí)現(xiàn)從按鈕點(diǎn)擊到數(shù)據(jù)處理和輸出顯示的完整邏輯流程。當(dāng)用戶點(diǎn)擊“轉(zhuǎn)換”按鈕時(shí),應(yīng)用程序?qū)⒔邮沼脩糨斎氲臄?shù)據(jù),并計(jì)算出轉(zhuǎn)換結(jié)果,然后顯示在界面上。
對應(yīng)用程序進(jìn)行測試和調(diào)試,確保應(yīng)用程序在Android設(shè)備上正常運(yùn)行。
??總之,使用Android Studio進(jìn)行開發(fā),可以很方便地實(shí)現(xiàn)一個(gè)米和英尺轉(zhuǎn)換器。在開發(fā)過程中,需要注意界面布局、事件處理、數(shù)據(jù)處理、算法實(shí)現(xiàn)等方面的細(xì)節(jié),以實(shí)現(xiàn)一個(gè)穩(wěn)定、高效、易用的移動(dòng)應(yīng)用程序。同時(shí),可以根據(jù)需求進(jìn)行功能擴(kuò)展和用戶體驗(yàn)優(yōu)化,以滿足不同用戶的需求和要求。
部分核心代碼
<!-- 輸入文本框 -->
<EditText
android:id="@+id/edit_text"
android:layout_width="286dp"
android:layout_height="85dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Enter meters here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.346" />
<!-- 圖片視圖 -->
<ImageView
android:id="@+id/imageView2"
android:layout_width="408dp"
android:layout_height="213dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/image" />
<!-- 按鈕控件 -->
<Button
android:id="@+id/convert_button"
android:layout_width="363dp"
android:layout_height="63dp"
android:text="CONVERT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- 顯示文本框 -->
<TextView
android:id="@+id/result_text"
android:layout_width="348dp"
android:layout_height="121dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.38"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.693" />
// 按鈕的轉(zhuǎn)換操作
converbutton.setOnClickListener((v) ->{
String input = edittext.getText().toString();
double value = Double.parseDouble(input);
double result = value * miToFT;
resulttext.setText(String.format("%.2f英尺",result));
resulttext.setVisibility(View.VISIBLE);
});
基本效果圖
??說明
:實(shí)驗(yàn)要求完成的部分基本完成,只是簡單的實(shí)現(xiàn)單位的轉(zhuǎn)換功能,右下角的那個(gè)圖片屬于拓展部分的,適用于通過點(diǎn)擊圖片實(shí)現(xiàn)為頁面添加背景顏色的功能,下面就簡單說一下怎么實(shí)現(xiàn)這一功能吧。
拓展部分簡單分析
??如果需求是文本圖片下方顯示一段紅色的固定背景,在用戶點(diǎn)擊圖片時(shí),背景不需要變化,可以采用以下步驟實(shí)現(xiàn):
在原有的項(xiàng)目基礎(chǔ)上,添加一個(gè)ImageView和一個(gè)TextView控件。選定需要帶有紅色背景的TextView,將其位置設(shè)置在ImageView的下方。
在Java代碼中,為TextView控件設(shè)置背景顏色。
將TextView的背景顏色設(shè)置為紅色,以顯示紅色的背景。
相關(guān)代碼的實(shí)現(xiàn)
<TextView
android:id="@+id/result_text"
android:layout_width="348dp"
android:layout_height="121dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.38"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.693" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="200dp"
android:layout_height="141dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.886"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView2"
app:layout_constraintVertical_bias="0.957"
app:srcCompat="@drawable/image1" />
imageView.setOnClickListener((V)->{
int color = Color.argb(128,255,0,0);
mainview.setBackgroundColor(color);
});
??代碼說明
:找到需要設(shè)置背景色的控件,然后使用Color.argb方法生成一個(gè)Argb顏色值,其中第一個(gè)參數(shù)128指定了顏色的透明度為50%
(255是不透明,0是全透明),后面三個(gè)參數(shù)分別為紅、綠、藍(lán)的顏色值
。最后使用setBackgroundColor方法將顏色值設(shè)置為控件的背景色
即可,需要注意的是,不同的設(shè)備可能會(huì)有不同的透明度支持,因此需要進(jìn)行實(shí)測調(diào)試,以保證透明效果符合需求。
基本實(shí)現(xiàn)效果圖
??再次申明本次實(shí)驗(yàn)是自己的Android Studio實(shí)驗(yàn)作業(yè),題目出自帥帥老師,上面的只是部分核心代碼的實(shí)現(xiàn),很簡單,因?yàn)樽约阂彩堑谝淮螌W(xué)習(xí)Android Studio,本身Java基礎(chǔ)就不怎么好,上面的代碼僅供參考,基本的bug已經(jīng)修復(fù),運(yùn)行是沒有問題的。有問題或者代碼還有可以改進(jìn)的地方可以一起學(xué)習(xí)交流,歡迎大佬指正~
bug問題1描述
??上面的實(shí)現(xiàn)過程中有一個(gè)問題不知道大家有沒有發(fā)現(xiàn),這個(gè)也是我調(diào)試多次后發(fā)現(xiàn)這個(gè)地方可以改進(jìn)一下,就是那個(gè)輸入文本框中的內(nèi)容,在用戶每次輸入數(shù)值的時(shí)候,都必須將里面的提示內(nèi)容全部清除掉才可以進(jìn)行輸入新的內(nèi)容,所以顯得很麻煩,理想的狀態(tài)應(yīng)該是用戶通過點(diǎn)擊輸入文本框的時(shí)候,就會(huì)自動(dòng)將里面的提示內(nèi)容隱藏掉,直接輸入新的內(nèi)容。
修改bug2的過程
<EditText
android:id="@+id/edit_text"
android:layout_width="286dp"
android:layout_height="85dp"
android:ems="10"
android:inputType="text"
android:hint="Enter meters here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.346" />
??說明
:其中,android:hint屬性表示提示文本的內(nèi)容
,可以根據(jù)需求自行修改。此外,android:inputType屬性指定了EditText控件的輸入類型
。在上述代碼中,輸入類型設(shè)置為text,表示接受任意文本輸入。可以根據(jù)需求設(shè)置對應(yīng)的輸入類型,例如數(shù)字、日期、郵箱等。當(dāng)用戶點(diǎn)擊EditText控件時(shí),提示文本會(huì)自動(dòng)隱藏,用戶可以直接在輸入框內(nèi)輸入內(nèi)容。需要注意的是,在設(shè)置EditText控件的輸入類型時(shí),可能會(huì)受到輸入法的影響,不同的輸入法可能會(huì)有不同的輸入效果。為避免輸入錯(cuò)誤,建議在需要輸入特定類型的文本時(shí),通過設(shè)置輸入類型及相關(guān)的輸入限制,來確保輸入的正確性。
bug問題2描述
??前一個(gè)問題就是一個(gè)簡單的小問題,大不了用戶刪除內(nèi)容重新進(jìn)行修改就可以了,但是還有一個(gè)致命的問題就是在輸入文本框中,當(dāng)用戶輸入直接的數(shù)字,運(yùn)行是沒有問題的,但是當(dāng)用戶輸入12米或者13m的時(shí)候,程序就會(huì)閃崩,既然提前注意到了,下面是自己嘗試修改的過程。
修改bug2的過程
converbutton.setOnClickListener((v) ->{
String input = edittext.getText().toString();
String regex = "\\d+(\\.\\d+)?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String numberStr = matcher.group();
double number = Double.parseDouble(numberStr);
double result = number * miToFT;
resulttext.setText(String.format("%.2f英尺", result));
resulttext.setVisibility(View.VISIBLE);
}
});
實(shí)現(xiàn)過程
??以上代碼中,首先
通過findViewById方法找到EditText控件
,并獲取其文本內(nèi)容
。然后定義一個(gè)正則表達(dá)式來匹配數(shù)字
,使用Pattern類和Matcher類提取EditText中的數(shù)字
,并逐個(gè)進(jìn)行處理。需要注意的是,在實(shí)際應(yīng)用中,還需要針對不同情況進(jìn)行不同的處理,例如判斷用戶輸入的數(shù)字單位是否符合要求,以及輸入的數(shù)字是否為0等。同時(shí),在使用正則表達(dá)式提取數(shù)字時(shí),也需要考慮一些邊界情況,例如小數(shù)點(diǎn)的位置和多次數(shù)字輸入等等,但是本次實(shí)驗(yàn)就是一個(gè)簡單的實(shí)現(xiàn)過程,不需要考慮這么多哦。
修改后的效果圖
??因?yàn)樯瞎?jié)課的后端正好也講了正則表達(dá)式了,感覺終于派上用場了,就簡單的用了一下,嘿嘿嘿,小小的成就感,現(xiàn)在沒有問題了,無論你輸入XXm或者XX.XX m都可以正確的運(yùn)行出對應(yīng)的英尺哦,好了,本次實(shí)驗(yàn)已完成,大的問題幾乎修改完了!
文章來源:http://www.zghlxwxcb.cn/news/detail-407197.html
????記錄是為了不停的思考,創(chuàng)作更是為了更好的思考
,有人說過:2023年以后的中國市場將永遠(yuǎn)屬于長期主義者,bug是改不完的也是寫不完的,只能說這次遇到了希望下次不會(huì)在出現(xiàn)同樣的bug就行,如果你覺得對您有幫助,歡迎一起交流學(xué)習(xí),本人也是一名很菜的計(jì)算機(jī)大三學(xué)生,目前還在努力中~文章來源地址http://www.zghlxwxcb.cn/news/detail-407197.html
到了這里,關(guān)于Android Studio:一個(gè)簡單的米英尺單位轉(zhuǎn)化app的實(shí)現(xiàn)過程的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!