Changes between Version 1 and Version 2 of wade/android/example_1


Ignore:
Timestamp:
Jun 21, 2011, 12:35:07 AM (13 years ago)
Author:
wade
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • wade/android/example_1

    v1 v2  
    11[[PageOutline]]
    22= source code =
     3{{{
     4#!java
     5package www.google.com;
     6import android.app.Activity;
     7import android.os.Bundle;
     8import android.view.View;
     9import android.view.View.OnClickListener;
     10import android.widget.Button;
     11import android.widget.EditText;
     12
     13public class Android_helloActivity extends Activity implements OnClickListener
     14{
     15    private EditText w_editText;                    // 存放 postfix 及目前輸入數值
     16    private EditText w_editText2;                   // 顯示結果               
     17    private String w_editTextString = "";           // w_editText 內的字串
     18    private String[] postfixStr = new String[100];  // postfix 運算式
     19    private int postfixPointer = 0;                 
     20    private String[] opStack = new String [100];    // 運算子的 stack
     21    private int opStackPointer = 0;
     22    private String op = "";
     23
     24    /** Called when the activity is first created. */
     25    @Override
     26    public void onCreate(Bundle savedInstanceState)
     27    {
     28        super.onCreate(savedInstanceState);
     29        setContentView(R.layout.main);
     30
     31        w_editText = (EditText) findViewById(R.id.editText1);
     32        w_editText2 = (EditText) findViewById(R.id.editText2);
     33       
     34        // 將 button 事件加入
     35        ((Button) findViewById(R.id.button0)).setOnClickListener(this); // 0
     36        ((Button) findViewById(R.id.button1)).setOnClickListener(this); // 1
     37        ((Button) findViewById(R.id.button2)).setOnClickListener(this); // 2
     38        ((Button) findViewById(R.id.button3)).setOnClickListener(this); // 3
     39        ((Button) findViewById(R.id.button4)).setOnClickListener(this); // 4
     40        ((Button) findViewById(R.id.button5)).setOnClickListener(this); // 5
     41        ((Button) findViewById(R.id.button6)).setOnClickListener(this); // 6
     42        ((Button) findViewById(R.id.button7)).setOnClickListener(this); // 7
     43        ((Button) findViewById(R.id.button8)).setOnClickListener(this); // 8
     44        ((Button) findViewById(R.id.button9)).setOnClickListener(this); // 9
     45        ((Button) findViewById(R.id.button10)).setOnClickListener(this); // +
     46        ((Button) findViewById(R.id.button11)).setOnClickListener(this); // -
     47        ((Button) findViewById(R.id.button12)).setOnClickListener(this); // ×
     48        ((Button) findViewById(R.id.button13)).setOnClickListener(this); // ÷
     49        ((Button) findViewById(R.id.button14)).setOnClickListener(this); // =
     50        ((Button) findViewById(R.id.button15)).setOnClickListener(this); // AC
     51    }
     52
     53    private boolean isOp(String str)
     54    {
     55        if (str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/"))
     56            return true;
     57        return false;
     58    }
     59   
     60   
     61    /** 計算結果 */
     62    private int compute(String[] seqStr, int size)
     63    {
     64        int i;
     65        int numP = 0;
     66        int[] num = new int[10];
     67       
     68        for (i = 0; i < size; i++)
     69        {
     70            if (isOp(seqStr[i]))
     71            {
     72                numP--;
     73                if (seqStr[i].equals("+"))
     74                    num[numP - 1] = num[numP - 1] + num[numP];
     75                else if (seqStr[i].equals("-"))
     76                    num[numP - 1] = num[numP - 1] - num[numP];
     77                else if (seqStr[i].equals("*"))
     78                    num[numP - 1] = num[numP - 1] * num[numP];
     79                else if (seqStr[i].equals("/"))
     80                    num[numP - 1] = num[numP - 1] / num[numP];
     81                else
     82                    return -9999;  // error
     83            }
     84            else
     85                num[numP++] =Integer.parseInt(seqStr[i]);
     86        }
     87        return num[0];
     88    }
     89
     90    /** click 事件  */
     91    @Override
     92    public void onClick(View view)
     93    {
     94        int i;
     95        switch (view.getId())
     96        {
     97            // 按鍵 0~9 事件
     98            case R.id.button0:
     99            case R.id.button1:
     100            case R.id.button2:
     101            case R.id.button3:
     102            case R.id.button4:
     103            case R.id.button5:
     104            case R.id.button6:
     105            case R.id.button7:
     106            case R.id.button8:
     107            case R.id.button9:
     108                // 將 android:text 的值取出
     109                String inDigit = ((Button) view).getText().toString();
     110                if (w_editTextString.equals("0")) {
     111                    w_editTextString = inDigit;
     112                } else {
     113                    w_editTextString += inDigit;
     114                }
     115                w_editText.setText(w_editTextString);
     116                break;
     117
     118            case R.id.button10:     // 按鍵 +
     119                // 存入數值
     120                postfixStr[postfixPointer++] = w_editTextString;
     121                w_editTextString = "";
     122                // 將 op stack 全取出
     123                if (!op.equals(""))
     124                {
     125                    for (i = --opStackPointer; i >= 0; i--)
     126                    {
     127                        postfixStr[postfixPointer++] = opStack[i];
     128                    }
     129                    opStackPointer = 0;
     130                }
     131                // 將新的 op 放入 stack
     132                opStack[opStackPointer++] = "+";
     133                op = "+";
     134                break;
     135            case R.id.button11:     // 按鍵 -
     136                // 存入數值
     137                postfixStr[postfixPointer++] = w_editTextString;
     138                w_editTextString = "";
     139                // 將 op stack 全取出
     140                if (!op.equals(""))
     141                {
     142                    for (i = --opStackPointer; i >= 0; i--)
     143                    {
     144                        postfixStr[postfixPointer++] = opStack[i];
     145                    }
     146                    opStackPointer = 0;
     147                }
     148                // 將新的 op 放入 stack
     149                opStack[opStackPointer++] = "-";
     150                op = "-";
     151                break;
     152            case R.id.button12:     // 按鍵 *
     153                // 存入數值
     154                postfixStr[postfixPointer++] = w_editTextString;
     155                w_editTextString = "";
     156                // 將新的 op 放入 stack
     157                opStack[opStackPointer++] = "*";
     158                op = "*";
     159                break;
     160            case R.id.button13:     // 按鍵 /
     161                // 存入數值
     162                postfixStr[postfixPointer++] = w_editTextString;
     163                w_editTextString = "";
     164                // 將新的 op 放入 stack
     165                opStack[opStackPointer++] = "/";
     166                op = "/";
     167                break;
     168            case R.id.button14:     // 按鍵 =
     169                postfixStr[postfixPointer++] = w_editTextString;
     170                w_editTextString = "";
     171               
     172                for (i = --opStackPointer; i >= 0; i--)
     173                {
     174                    postfixStr[postfixPointer++] = opStack[i];
     175                }
     176                //w_editText.setText(" "+postfixPointer);
     177                for (i = 0; i < postfixPointer; i++)
     178                {
     179                    w_editTextString = w_editTextString + postfixStr[i] + " ";
     180                }
     181                w_editText.setText(w_editTextString);
     182                w_editText2.setText(Integer.toString(compute(postfixStr, postfixPointer)));
     183                break;
     184       
     185            // 按鍵 AC :回到初始值
     186            case R.id.button15:
     187                w_editTextString = "";  // w_editText 內的字串
     188                postfixPointer = 0;                 
     189                opStackPointer = 0;
     190                op = "";
     191                w_editText.setText("");
     192                w_editText2.setText("");
     193                break;
     194        }   // end of switch
     195    }   // edn of onClick
     196
     197}
     198}}}
    3199= layout xml =
     200{{{
     201#!xml
     202<?xml version="1.0" encoding="utf-8"?>
     203<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     204    android:layout_width="fill_parent"
     205    android:layout_height="fill_parent" android:orientation="vertical">
     206    <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content">
     207        <requestFocus></requestFocus>
     208    </EditText>
     209    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
     210        <Button android:text="7" android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     211        <Button android:text="8" android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     212        <Button android:text="9" android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     213        <Button android:text="+" android:id="@+id/button10" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     214    </LinearLayout>
     215    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content">
     216        <Button android:text="4" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     217        <Button android:text="5" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     218        <Button android:text="6" android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     219        <Button android:text="–" android:id="@+id/button11" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     220    </LinearLayout>
     221    <LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="wrap_content">
     222        <Button android:text="1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     223        <Button android:text="2" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     224        <Button android:text="3" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     225        <Button android:text="*" android:id="@+id/button12" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     226    </LinearLayout>
     227    <LinearLayout android:id="@+id/linearLayout4" android:layout_width="match_parent" android:layout_height="wrap_content">
     228        <Button android:text="0" android:id="@+id/button0" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     229        <Button android:text="AC" android:id="@+id/button15" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     230        <Button android:text="=" android:id="@+id/button14" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     231        <Button android:text="/" android:id="@+id/button13" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
     232    </LinearLayout>
     233    <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content"></EditText>
     234 
     235</LinearLayout>
     236
     237}}}