实训4 事件监听器2
一、【实训目的】
(1) 事件监听三要素
(2) 事件监听常用方法
(3) 事件监听器
二、【实训步骤和要求】
1. 利用事件处理,写一个简单的计算器,如下图所示:
源代码:
MainActivity.java源代码:
package com.example.tt3;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText number1,number2;
private Button bt1;
private float n1,n2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 2、变量关联布局控件
number1 = (EditText)findViewById(R.id.number1);
number2= (EditText)findViewById(R.id.number2);
bt1 = (Button)findViewById(R.id.button1);
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
n1=Float.parseFloat(number1.getText().toString());
n2=Float.parseFloat(number2.getText().toString());
/*Bundle bd1 = new Bundle();
bd1.putFloat("n1", n1);
bd1.putFloat("n2", n2);*/
Float a;
a=n1+n2;
String n3;
n3=String.valueOf(a);
TextView textView = (TextView) findViewById(R.id.number3);
textView.setText(n3);
}
}); // 1、事件源添加事件监听
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
string.xml源程序:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TT3</string>
<string name="action_settings">Settings</string>
<string name="k1">0</string>
<string name="k2">+</string>
<string name="k3"></string>
<string name="k4">=</string>
<string name="k5"></string>
</resources>
Activity_main.xml源代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#eee"
android:layout_marginBottom="10dp">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/number1"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:text="@string/k1"
android:layout_weight="1"/>
<TextView
android:id="@+id/add"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:text="@string/k2"/>
<EditText
android:id="@+id/number2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:text="@string/k3"
android:layout_weight="1"/>
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:text="@string/k4"/>
<EditText
android:id="@+id/number3"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:hint="@null"
android:text="@string/k5"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</LinearLayout>
项目成果:
2、写一个计算器,如下图,实现简单的加减乘除运算。
Activity_main.xml源代码:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:rowCount="6"
android:columnCount="4"
> <!--设置网格为6行4列-->
<!--显示文本组件,占1行4列-->
<TextView
android:id="@+id/text"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:layout_marginLeft="4px"
android:gravity="left"
android:textSize="50dp" />
<!--清除按钮,占1行4列-->
<Button
android:id="@+id/btnClear"
android:layout_width="353dp"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:text="清除"
android:textSize="26sp" />
<!--以下按钮为数字按钮和函数按钮,每个占1行1列-->
<Button
android:id="@+id/btn1"
android:text="1"
android:textSize="26sp" />
<Button
android:id="@+id/btn2"
android:text="2"
android:textSize="26sp" />
<Button
android:id="@+id/btn3"
android:text="3"
android:textSize="26sp" />
<Button
android:id="@+id/btnPlus"
android:text="+"
android:textSize="26sp" />
<Button
android:id="@+id/btn4"
android:text="4"
android:textSize="26sp" />
<Button
android:id="@+id/btn5"
android:text="5"
android:textSize="26sp" />
<Button
android:id="@+id/btn6"
android:text="6"
android:textSize="26sp" />
<Button
android:id="@+id/btnSubtract"
android:text="-"
android:textSize="26sp" />
<Button
android:id="@+id/btn7"
android:text="7"
android:textSize="26sp" />
<Button
android:id="@+id/btn8"
android:text="8"
android:textSize="26sp" />
<Button
android:id="@+id/btn9"
android:text="9"
android:textSize="26sp" />
<Button
android:id="@+id/btnMultiply"
android:text="*"
android:textSize="26sp" />
<Button
android:id="@+id/btnPoint"
android:text="."
android:textSize="26sp" />
<Button
android:id="@+id/btn0"
android:text="0"
android:textSize="26sp" />
<Button
android:id="@+id/btnSum"
android:text="="
android:textSize="26sp" />
<Button
android:id="@+id/btnDivide"
android:text="/"
android:textSize="26sp" />
</GridLayout>
MainActivity.java源代码:
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
btnClear,btnPlus,btnSubtract,btnMultiply,btnDivide,btnSum,btnPoint;
TextView text;
String str = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
btnClear = (Button) findViewById(R.id.btnClear);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnSubtract = (Button) findViewById(R.id.btnSubtract);
btnMultiply = (Button) findViewById(R.id.btnMultiply);
btnDivide = (Button) findViewById(R.id.btnDivide);
btnPoint = (Button) findViewById(R.id.btnPoint);
btnSum = (Button) findViewById(R.id.btnSum);
text = (TextView) findViewById(R.id.text) ;
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);
btnClear.setOnClickListener(this);
btnPlus.setOnClickListener(this);
btnSubtract.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
btnPoint.setOnClickListener(this);
btnSum.setOnClickListener(new click());
//给所有按钮注册点击事件
}
@Override
public void onClick(View v) {
String input=text.getText().toString();
switch (v.getId()){
case R.id.btn0:
case R.id.btn1:
case R.id.btn2:
case R.id.btn3:
case R.id.btn4:
case R.id.btn5:
case R.id.btn6:
case R.id.btn7:
case R.id.btn8:
case R.id.btn9:
case R.id.btnPoint:
text.setText(input+((Button)v).getText());
break;
case R.id.btnPlus:
case R.id.btnSubtract:
case R.id.btnMultiply:
case R.id.btnDivide:
text.setText(input + " " + ((Button)v).getText() + " ");
//给运算符前后加空格,好判断
break;
case R.id.btnClear:
text.setText("");
break;
}
}
class click implements View.OnClickListener {
public void onClick (View v) {
getResult();
}
}
private void getResult () {
String str1 = text.getText().toString();
if(str1 == null || str1.equals("")){
return;
}
if(!str1.contains(" ")){
return ;
}
double result = 0;
// 第一个数的字符串
String s1 = str1.substring(0,str1.indexOf(" "));
// 运算符
String op = str1.substring(str1.indexOf(" ")+1,str1.indexOf(" ")+2);
// 第二个数的字符串
String s2 = str1.substring(str1.indexOf(" ")+3);
double d1 = Double.parseDouble(s1);//将数字字符串转为double类型
double d2 = Double.parseDouble(s2);
if (op.equals("+")) { //加法运算
result = d1 + d2;
} else if (op.equals("-")) { //减法运算
result = d1 - d2;
} else if (op.equals("*")) { //乘法运算
result = d1 * d2;
} else if (op.equals("/")) { //除法运算
if (d2 == 0) { //如果被除数是0
result = 0; //则结果是0
}
else {
result = d1 / d2;
}
}
text.setText(str1 + " = " + result); //显示计算结果
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {
//如果两个整数且不是出发运算
int r = (int) result; //则结果转为整数
text.setText(str1 + " = " + r );
}
}
}
项目成果:
附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考!