效果如下:
点击菜单后:
点击关于:
一共建了两个包(package),一个Activity,一个算法,使得代码更简洁。
使用了Menu和Dialog。
activity包中:
MainActivity.java
package cn.edu.bzu.counter.activity; import cn.edu.bzu.counter.algorithm.Algorithm; import cn.edu.bzu.test2.R; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { EditText input1; EditText input2; Button jia; Button jian; Button cheng; Button chu; TextView fuhao; TextView re; String in , in2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init(){ input1 = (EditText)findViewById(R.id.input_1); input2 = (EditText)findViewById(R.id.input_2); jia = (Button)findViewById(R.id.jia); jian = (Button)findViewById(R.id.jian); cheng = (Button)findViewById(R.id.cheng); chu = (Button)findViewById(R.id.chu); fuhao = (TextView)findViewById(R.id.fuhao); re = (TextView)findViewById(R.id.RE); } public void jia(View view){ boolean b = getNumer(); if(b==false) return;//如果为空,则停止 fuhao.setText("+"); double a =Algorithm.jiafa(in, in2); re.setText(a+""); } public void jian(View view){ boolean b = getNumer(); if(b==false) return;//如果为空,则停止 fuhao.setText("-"); double a =Algorithm.jianfa(in, in2); re.setText(a+""); } public void cheng(View view){ boolean b = getNumer(); if(b==false) return;//如果为空,则停止 fuhao.setText("*"); double a =Algorithm.chengfa(in, in2); re.setText(a+""); } public void chu(View view){ boolean b = getNumer(); if(b==false) return; //如果为空,则停止 fuhao.setText("/"); double a =Algorithm.chufa(in, in2); re.setText(a+""); } /** * 获取文本并检查是否为空 */ public boolean getNumer(){ in = input1.getText().toString(); in2 = input2.getText().toString(); if(in.equals("")||in2.equals("")){ Toast.makeText(MainActivity.this,"请输入", Toast.LENGTH_SHORT).show(); return false; } return true; } /** * 设置菜单的点击事件 */ @Override public boolean onOptionsItemSelected(MenuItem item) { int i = item.getItemId(); switch (i) { //点击关于,弹出对话框 case R.id.about: AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); alertDialog.setTitle("说明") .setMessage("作者:0000"+"\n"+"版本:111.0.0.0") .setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int arg1) { dialog.dismiss(); } }).show(); break; case R.id.exit: MainActivity.this.finish(); break; } //return super.onOptionsItemSelected(item); return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } }
算法包:
package cn.edu.bzu.counter.algorithm; import android.content.Context; import android.widget.Toast; public class Algorithm { public static double jiafa(String in, String in2){ double a =Double.parseDouble(in); double b = Double.parseDouble(in2); return a+b; } public static double jianfa(String in, String in2){ double a =Double.parseDouble(in); double b = Double.parseDouble(in2); return a-b; } public static double chengfa(String in, String in2){ double a =Double.parseDouble(in); double b = Double.parseDouble(in2); return a*b; } /** * 抛出异常,防止除数为0 */ public static double chufa(String in, String in2) throws ArithmeticException{ double a =Double.parseDouble(in); double b = Double.parseDouble(in2); return a/b; } }
menu文件中的main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/about" android:title="关于"/> <item android:id="@+id/exit" android:title="退出"/> </menu>
布局文件(使用了多个Linerlayout嵌套):
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="80dp" android:orientation="horizontal" > <EditText android:id="@+id/input_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/fuhao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="+" android:textSize="30sp" /> <EditText android:id="@+id/input_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="=" android:textSize="35sp" /> <TextView android:id="@+id/RE" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" > <Button android:id="@+id/jia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="jia" android:text="+" android:textSize="30dp" /> <Button android:id="@+id/jian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="jian" android:text="-" android:textSize="30dp" /> <Button android:id="@+id/cheng" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="cheng" android:text="*" android:textSize="30dp" /> <Button android:id="@+id/chu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="chu" android:text="/" android:textSize="30dp" /> </LinearLayout> </LinearLayout>
遇到问题:
在onOptionsItemSelected方法中,语句不报错,检测时对话框就是弹不出来,仔细观察,缺少了一个show()方法。