自學(xué)鴻蒙應(yīng)用開發(fā)(21)- 分組處理按鈕操作
計(jì)算器程序的第一步是首先輸入需要求值的表達(dá)式,以下是本款計(jì)算器軟件輸入表達(dá)式時(shí)的狀態(tài):

分組處理按鈕
計(jì)算器差不多是按鈕最多的應(yīng)用程序,因此如何處理這些按鈕就成了必須解決的一個(gè)問題。在本軟件中我們采用分組方式簡(jiǎn)化按鈕的處理。
對(duì)于大多數(shù)按鈕我們只要將按鈕的內(nèi)容文字添加到求值表達(dá)式即可;對(duì)于函數(shù)功能也可以按相同方式處理,但是為了更加簡(jiǎn)化輸入過程,除了添加函數(shù)名以外,后面再加一個(gè)左括號(hào)。
首先是直接輸入的情況:
private void prepareDirectionButtons(){int direct_button[] = {//token_areaResourceTable.Id_i_button,ResourceTable.Id_angle_button,ResourceTable.Id_degree_button,ResourceTable.Id_left_parentheses_button,ResourceTable.Id_comma_button,ResourceTable.Id_right_parentheses_button,ResourceTable.Id_sharp_button,//number_areaResourceTable.Id_number0_button,ResourceTable.Id_number1_button,ResourceTable.Id_number2_button,ResourceTable.Id_number3_button,ResourceTable.Id_number4_button,ResourceTable.Id_number5_button,ResourceTable.Id_number6_button,ResourceTable.Id_number7_button,ResourceTable.Id_number8_button,ResourceTable.Id_number9_button,ResourceTable.Id_plus_button,ResourceTable.Id_minus_button,ResourceTable.Id_mul_button,ResourceTable.Id_div_button,ResourceTable.Id_dot_button,ResourceTable.Id_exp_button,ResourceTable.Id_percent_button,};for(int id:direct_button){Button button = (Button) findComponentById(id);button.setClickedListener(new Component.ClickedListener() {public void onClick(Component v) {appendQuestionString(((Button)v).getText());}});}}
代碼中將所有需要相同處理的按鈕保存到一個(gè)數(shù)組中,然后使用一個(gè)循環(huán)結(jié)構(gòu)為每個(gè)按鈕增加相同的處理代碼。
對(duì)于標(biāo)準(zhǔn)函數(shù)按鈕,處理方式和內(nèi)容與標(biāo)準(zhǔn)按鈕大致相同,只是在最后向表達(dá)式增加內(nèi)容時(shí)多輸入一個(gè)左括號(hào):
private void prepareFunButtons(){int std_fun_button[] = {//fun_area1ResourceTable.Id_sin_button,ResourceTable.Id_cos_button,ResourceTable.Id_tan_button,ResourceTable.Id_asin_button,ResourceTable.Id_acos_button,ResourceTable.Id_atan_button,//fun_area2ResourceTable.Id_x2_button,ResourceTable.Id_x3_button,ResourceTable.Id_sqrt_button,ResourceTable.Id_subtriplicate_button,ResourceTable.Id_power_button,ResourceTable.Id_root_button,};for(int id:std_fun_button){Button button = (Button) findComponentById(id);button.setClickedListener(new Component.ClickedListener() {public void onClick(Component v) {appendQuestionString(((Button)v).getText() + "(");}});}}
數(shù)量最多的兩類按鈕的處理完成之后,再加上一個(gè)退格鍵和和清除鍵,我們今天演示的動(dòng)作就基本完成了。
Button back_button = (Button)findComponentById(ResourceTable.Id_back_button);back_button.setClickedListener(new Component.ClickedListener() {public void onClick(Component v) {backQuestion();}});Button ac_button = (Button)findComponentById(ResourceTable.Id_ac_button);ac_button.setClickedListener(new Component.ClickedListener() {public void onClick(Component v) {clearQuestion();}});
最后就是向表達(dá)式中增加內(nèi)容,退格和清除三個(gè)方法的具體實(shí)現(xiàn),它們都很簡(jiǎn)單:
private void appendQuestionString(String str){TextField question = (TextField)findComponentById(ResourceTable.Id_question_field);question.setText(question.getText() + str);}private void backQuestion(){TextField question = (TextField)findComponentById(ResourceTable.Id_question_field);String current = question.getText();if(current.length() > 0)question.setText(current.substring(0, current.length() - 1));}private void clearQuestion(){TextField question = (TextField)findComponentById(ResourceTable.Id_question_field);question.setText("");}
以下是動(dòng)作視頻:
關(guān)于計(jì)算器的具體實(shí)現(xiàn),讀者也可以參照下面的《實(shí)戰(zhàn)Python設(shè)計(jì)模式》一書,書中有本文正在介紹的計(jì)算器的設(shè)計(jì)思路,只是具體實(shí)現(xiàn)時(shí)使用了Python語(yǔ)言。
新書介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問題;另一方面通過對(duì)這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。
對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語(yǔ)言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
