Android開發牛刀小試之“AA算錢軟體”開發(二)

來源:互聯網
上載者:User

標籤:android開發   layout   app   

今天主要修改了幾個bug,然後改了下背景圖片和表徵圖,添加了變動人數的功能,即:指定參與AA的人數,參與AA計算。還有就是利用getResources().getIdentifier()解決了迴圈調用layout中組件id的問題。

下面是幾段核心代碼:

private class OnClickListenerIreture implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//首先擷取NUMBER,也就是要指定的參與AA的人數EditText edt = (EditText) AAcharge.this.findViewById(R.id.ednum);NUMBER = Integer.parseInt(edt.getText().toString());int stuid[] = new int[NUMBER];TextView stu[] = new TextView[NUMBER];for (int k =1;k<stu.length+1;k++){//get coststuid[k-1] = getResources().getIdentifier("stu"+k, "id", getPackageName());stu[k-1] = (TextView) AAcharge.this.findViewById(stuid[k-1]);}double num[] = new double[NUMBER];int payid[] = new int[NUMBER];int nameid[] = new int[NUMBER];Person p[] = new Person[NUMBER];EditText ed[] = new EditText[NUMBER];for (int i = 0;i<NUMBER;i++){p[i] = new Person();ed[i] = null;}for (int k =1;k<num.length+1;k++){//get costpayid[k-1] = getResources().getIdentifier("pay"+k, "id", getPackageName());ed[k-1] = (EditText) AAcharge.this.popView.findViewById(payid[k-1]);num[k-1] = Double.parseDouble(ed[k-1].getText().toString()); // 取得輸入的錢數p[k-1].setCost(num[k-1]);//get namenameid[k-1] = getResources().getIdentifier("myed"+k, "id", getPackageName());ed[k-1] = (EditText) AAcharge.this.popView.findViewById(nameid[k-1]);p[k-1].setName(ed[k-1].getText().toString());}double total =0;for (int t =0;t<p.length;t++){total = total + p[t].getCost();}double average = ((int)((total)*10/NUMBER))/10.0;AAcharge.this.statusinfo.setText("總花費:" +String.valueOf(total)+ "   平均花費:"+String.valueOf(average));AAcharge.this.popWin.dismiss();Calculate(average,p);for (int j =0;j<stu.length;j++){stu[j].setText(p[j].getInfo());}}}
//主要的AA計算的演算法,其實很簡單,首先計算均值,然後每個人付的錢減去均值存到數組中,然後再從小到大排序,之後取第一個非零值,用非零值前面的負值去填這個非零值,直到等於零,再加1.public void Calculate(double avg, Person p[]) {// TODO Auto-generated method stubint i =0;int j =0;int index=0;sort(p);for (int k =0;k<p.length;k++){p[k].setGetmoney(p[k].getCost()-avg);}index = findPosIndex(p);i=index;for (j = index -1;j>=0;){if (i<p.length){if ((p[i].getGetmoney()+p[j].getGetmoney())>0){p[j].setInfo(p[j].getName()+ "付給" + p[i].getName()+" : "+String.valueOf(Math.abs(p[j].getGetmoney()))+"元;"+"\n"+p[j].getInfo());p[i].setGetmoney(p[i].getGetmoney()+p[j].getGetmoney());j--;} else if ((p[i].getGetmoney()!=0) && ((p[i].getGetmoney()+p[j].getGetmoney())==0)){p[j].setInfo(p[j].getName()+ "付給" + p[i].getName()+" : "+String.valueOf(Math.abs(p[j].getGetmoney()))+"元;"+"\n"+p[j].getInfo());p[i].setGetmoney(p[i].getGetmoney()+p[j].getGetmoney());i++;j--;} else if((p[i].getGetmoney()+p[j].getGetmoney())<0){p[j].setInfo(p[j].getName()+ "付給" + p[i].getName()+" : "+String.valueOf(Math.abs(p[i].getGetmoney()))+"元;"+"\n"+p[j].getInfo());p[j].setGetmoney(p[i].getGetmoney()+p[j].getGetmoney());i++;}}}}private int findPosIndex(Person[] p) {// TODO Auto-generated method stubint index =0;for (int i =0;i<p.length;i++){if(p[i].getGetmoney()>0) {index = i;break;}}return index;}public void sort(Person p[]) {// TODO Auto-generated method stubPerson temp =new Person();for (int i =0;i<p.length-1;i++){for (int j =0;j<p.length-i-1;j++){if (p[j].getCost()>p[j+1].getCost()){temp = p[j];p[j] = p[j+1];p[j+1] = temp;}}}}}

最後是附上現在的app,有個girl說我太屌絲了,無所謂啦,我就是圖個開心。



Android開發牛刀小試之“AA算錢軟體”開發(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.