Форум программистов, компьютерный форум, киберфорум
Программирование Android
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
0 / 0 / 0
Регистрация: 17.12.2010
Сообщений: 13
1

Кнопочный калькулятор!!!

27.04.2012, 19:48. Показов 1571. Ответов 0
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Ребят, помогите плз... Изучаю азы андроида и хочу сделать кнопочный калькулятор. Вот что получилось.
Не могу переменной задать ту цифру которую нажали.
X - первая цифра
Y - вторая
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.sample.calculyator;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class Calculator2Activity extends Activity implements OnClickListener {
    
    TextView text;
    Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnPlus,btnMinus, btnYmn, btnDel,btnRavno, btnToch;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        text=(TextView)findViewById(R.id.text);
        
        btn0=(Button)findViewById(R.id.btn0);
        btn1=(Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        btn3=(Button)findViewById(R.id.btn3);
        btn4=(Button)findViewById(R.id.btn4);
        btn5=(Button)findViewById(R.id.btn5);
        btn6=(Button)findViewById(R.id.btn6);
        btn7=(Button)findViewById(R.id.btn7);
        btn8=(Button)findViewById(R.id.btn8);
        btn9=(Button)findViewById(R.id.btn9);
        btnDel=(Button)findViewById(R.id.btnDel);
        btnYmn=(Button)findViewById(R.id.btnYmn);
        btnPlus=(Button)findViewById(R.id.btnPlus);
        btnMinus=(Button)findViewById(R.id.btnMinus);
        btnRavno=(Button)findViewById(R.id.btnRavno);
        btnToch=(Button)findViewById(R.id.btnToch);
      
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnDel.setOnClickListener(this);
        btnYmn.setOnClickListener(this);
        btnPlus.setOnClickListener(this);
        btnMinus.setOnClickListener(this);
        btnRavno.setOnClickListener(this);
        btnToch.setOnClickListener(this);
        
    }
 
    public void onClick(View v) {
        
        String oper;
        float x,y,z;
        for(int i=0; i<1; i++){
        switch (v.getId()){
        case R.id.btn0: text.setText("0"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn1: text.setText("1"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn2: text.setText("2"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn3: text.setText("3"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn4: text.setText("4"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn5: text.setText("5"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn6: text.setText("6"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn7: text.setText("7"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn8: text.setText("8"); x=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn9: text.setText("9"); x=Float.parseFloat(text.getText().toString()); break;
        default: break;
        }}
        switch (v.getId()){
        case R.id.btnDel: text.setText("/"); oper=text.getText().toString(); break;
        case R.id.btnMinus: text.setText("-"); oper=text.getText().toString(); break;
        case R.id.btnPlus: text.setText("+"); oper=text.getText().toString(); break;
        case R.id.btnYmn: text.setText("*"); oper=text.getText().toString(); break;
        }
        
        for(int j=0; j<1; j++){
        switch (v.getId()){
        case R.id.btn0: text.setText("0"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn1: text.setText("1"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn2: text.setText("2"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn3: text.setText("3"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn4: text.setText("4"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn5: text.setText("5"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn6: text.setText("6"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn7: text.setText("7"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn8: text.setText("8"); y=Float.parseFloat(text.getText().toString()); break;
        case R.id.btn9: text.setText("9"); y=Float.parseFloat(text.getText().toString()); break;
        default: break;
        }}
        
    }
}

XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
 
        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
 
            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
 
                <TextView
                    android:id="@+id/text"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:text="TextView" android:layout_weight="1"/>
 
            </TableRow>
 
            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
 
                <Button
                    android:id="@+id/btn7"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="7" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btn8"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="8" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btn9"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="9" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btnPlus"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="+" 
                    android:layout_weight="1" 
                    android:textStyle="bold"/>
 
            </TableRow>
 
            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
 
                <Button
                    android:id="@+id/btn4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="4" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btn5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="5" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btn6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="6" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btnMinus"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="-" 
                    android:layout_weight="1" 
                    android:textStyle="bold"/>
 
            </TableRow>
 
            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
 
                <Button
                    android:id="@+id/btn1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="1" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btn2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="2" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btn3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="3" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btnYmn"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="*" 
                    android:layout_weight="1" 
                    android:textStyle="bold"/>
 
            </TableRow>
 
            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
 
                <Button
                    android:id="@+id/btn0"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="0" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btnToch"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="." 
                    android:layout_weight="1"/>
 
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="" 
                    android:layout_weight="1"/>
 
                <Button
                    android:id="@+id/btnDel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="/" 
                    android:layout_weight="1" 
                    android:textStyle="bold"/>
 
            </TableRow>
 
        </TableLayout>
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
 
            <Button
                android:id="@+id/btnRavno"
                android:layout_width="fill_parent"
                android:layout_height="94dp"
                android:text="=" android:textSize="60dp"/>
 
        </LinearLayout>
 
    </TableLayout>
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
27.04.2012, 19:48
Ответы с готовыми решениями:

Кнопочный калькулятор
Пишу кнопочный калькулятор Столкнулся со следующей проблемой: например я жму кнопки 1 2 3 4 5 и...

Кнопочный калькулятор С#
Вот собственно мой вариант калькулятора. Он работает. 3 текстбокса и 6 кнопок-действий с этими...

Кнопочный калькулятор (доделать возведение числа в степень и удаление последнего введенного символа)
Задание Выполнить проект &quot;кнопочный калькулятор&quot;в Delphi, предусмотреть: -выполнение основных...

Кнопочный телефон
Здравствуйте. Хочу купить простой, кнопочный телефон с 2 сим-картами, дизайн важен (склоняюсь к...

0
27.04.2012, 19:48
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
27.04.2012, 19:48
Помогаю со студенческими работами здесь

Кнопочный слайдер
Здравствуйте!Я в слайдерах полный ноль, а мне понадобилось создать слайдер...Ребята, помогите, если...

Кнопочный пульт
Здравствуйте. Хочу собрать пульт управления макетом на ардуине. Помогите пожалуйста написать скетч...

Кнопочный фильтр
Добрый день! Такой вопрос: есть форма (содержащая вопросы и ответы) и есть кнопка общего фильтра,...

Кнопочный андроид
Привет Подскажите есть ли в продаже смартфоны кнопочные, но на андроид? Ситуация такая на солнце...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
1
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru