计算机对话框--安卓作业3.3

一、实验要求

设计一个带有计算器功能的对话框

二、让我们先来看一下运行结果,方便我们清楚实验需求

 

三、源码分析

3.0 配置文件 -- AndroidManifest.xml(将CalActivity的页面主题改成对话框形式)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hp480.example.lianxi_3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".CalActivity"
            android:theme="@android:style/Theme.Dialog">
        </activity>

    </application>

</manifest>

3.1 布局文件 -- activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算机对话框"
        android:textSize="20sp"/>
</LinearLayout>

3.2 布局文件 -- computer.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="6"
    android:columnCount="4">
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_gravity="left"
        android:layout_marginLeft="4px"
        android:text=""
        android:textSize="50dip"
        android:background="@android:drawable/screen_background_light"/>
    <Button
        android:id="@+id/btn_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"
        android:textSize="26sp"/>
    <Button android:text="1" android:textSize="26sp" android:id="@+id/btn_1"/>
    <Button android:text="2" android:textSize="26sp" android:id="@+id/btn_2"/>
    <Button android:text="3" android:textSize="26sp" android:id="@+id/btn_3"/>
    <Button android:text="+" android:textSize="26sp" android:id="@+id/btn_plus"/>
    <Button android:text="4" android:textSize="26sp" android:id="@+id/btn_4"/>
    <Button android:text="5" android:textSize="26sp" android:id="@+id/btn_5"/>
    <Button android:text="6" android:textSize="26sp" android:id="@+id/btn_6"/>
    <Button android:text="-" android:textSize="26sp" android:id="@+id/btn_minus"/>
    <Button android:text="7" android:textSize="26sp" android:id="@+id/btn_7"/>
    <Button android:text="8" android:textSize="26sp" android:id="@+id/btn_8"/>
    <Button android:text="9" android:textSize="26sp" android:id="@+id/btn_9"/>
    <Button android:text="*" android:textSize="26sp" android:id="@+id/btn_multipy"/>
    <Button android:text="." android:textSize="26sp" android:id="@+id/btn_point"/>
    <Button android:text="0" android:textSize="26sp" android:id="@+id/btn_0"/>
    <Button android:text="=" android:textSize="26sp" android:id="@+id/btn_equal"/>
    <Button android:text="/" android:textSize="26sp" android:id="@+id/btn_divide"/>
</GridLayout>

3.3 控制文件 -- MainActivity.java

package hp480.example.lianxi_3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new mClick());
    }
    class mClick implements View.OnClickListener
    {
        @Override
        public void onClick(View arg0)
        {
            Intent intent = new Intent(MainActivity.this,CalActivity.class);
            startActivity(intent);
        }
    }

}

3.4 控制文件 -- CalActivity.java

package hp480.example.lianxi_3;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;

public class CalActivity extends Activity {
    Button btn_plus,btn_minus,btn_multipy,btn_divide,btn_clear,btn_equal;
    TextView tv;
    int[] buttons;
    Float result;
    Float result0;
    Float result1;
    String str1;
    String str2;
    int flag = 0;
    Button temp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.computer); // 跳转到main界面
        initButton();
        // 监听“清空按钮”
        btn_clear.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                str1 = "";
                str2 = ""; // 清空记录
                result=null;
                tv.setText(str1);
                flag = 0;
            }
        });
        // 监听每一个“数字button”
        for (int i = 0; i < buttons.length; i++) {
            temp = (Button) findViewById(buttons[i]);
            temp.setOnClickListener( // 为Button添加监听器
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            str2 = ((Button) v).getText().toString();// 获得新输入的值
                            tv.setText(tv.getText().toString().trim()+str2);
                            str1=str1+ str2;
                            Log.d("myTag", "str1" + ":::" + str1);
                        }
                    });
        }
        // 为“计算符号”添加事件,以标记
        buttonListener(btn_plus, 1);
        buttonListener(btn_minus, 2);
        buttonListener(btn_multipy, 3);
        buttonListener(btn_divide, 4);

        // 为“等于号”添加事件,得出结果
        btn_equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(result==null){
                    String str = tv.getText().toString().trim();  //拓展,改成获取每一个运算符前一个数字即可无限运算,还未实现
                    String h[] =str.split("\\+|\\-|\\*|\\/");
                    result=Float.parseFloat(h[0]);
                }
                Log.d("111:", String.valueOf(result));
                Log.d("222:", String.valueOf(str1));
                if (flag == 1) {
                    result = result + Float.parseFloat(str1);
                    Log.d("result:", String.valueOf(result));
                } else if (flag == 2) {
                    result = result - Float.parseFloat(str1);
                    Log.d("result:", String.valueOf(result));
                } else if (flag == 3) {
                    result = result * Float.parseFloat(str1);
                    Log.d("result:", String.valueOf(result));
                } else if (flag == 4) {
                    result = (Float) (result / Float.parseFloat(str1));
                    Log.d("result:", String.valueOf(result));
                }
                String str = (tv.getText().toString().trim()+String.valueOf(((Button) v).getText())+result).trim();
                Log.d("myTag",str);
                tv.setText(str);
            }
        });
    }

    // 初始化数字按钮
    public void initButton() { // 初始化控件资源
        tv = (TextView) this.findViewById(R.id.tv); // 获取文本框控件对象
        str1 = String.valueOf(tv.getText());
        str2 = ""; // 初始化运算输入数值

        btn_clear = (Button) this.findViewById(R.id.btn_clear); // 获得计算按钮的按钮对象
        btn_plus = (Button) this.findViewById(R.id.btn_plus);
        btn_minus = (Button) this.findViewById(R.id.btn_minus);
        btn_multipy = (Button) this.findViewById(R.id.btn_multipy);
        btn_divide = (Button) this.findViewById(R.id.btn_divide);
        btn_equal = (Button) this.findViewById(R.id.btn_equal);

        buttons = new int[] { // 记录数值按钮的id
                R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3,
                R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7,
                R.id.btn_8, R.id.btn_9 , R.id.btn_point};
    }

    // 监听计算符号的事件
    public void buttonListener(Button button, final int id) {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = tv.getText().toString().trim();  //拓展,改成获取每一个运算符前一个数字即可无限运算,还未实现
                String h[] =str.split("\\+|\\-|\\*|\\/");
                tv.setText(str+String.valueOf(((Button) v).getText()));
                if(h.length==1){
                    flag=id;
                }
                if(h.length>1){
                    Log.d("最后一个数字--------",h[h.length-1]);
                    str=h[h.length-1];
                    if(h.length==2){
//                        result=Float.parseFloat(h[h.length-2]);
                        if (flag == 1) {
                            result = Float.parseFloat(h[h.length-2]) + Float.parseFloat(str);
                            Log.d("result:",String.valueOf(result));
                        } else if (flag == 2) {
                            result = Float.parseFloat(h[h.length-2]) - Float.parseFloat(str);
                            Log.d("result:",String.valueOf(result));
                        } else if (flag == 3) {
                            result = Float.parseFloat(h[h.length-2]) * Float.parseFloat(str);
                            Log.d("result:",String.valueOf(result));
                        } else if (flag == 4) {
                            result = Float.parseFloat(h[h.length-2]) / Float.parseFloat(str);
                            Log.d("result:",String.valueOf(result));
                        }
                    }else{//                result1 = Float.parseFloat(str1);
                        if (flag == 1) {
                            Log.d("运算过程:",result + "+" + Float.parseFloat(str));
                            result = result + Float.parseFloat(str);
                            Log.d("result:", String.valueOf(result));
                        } else if (flag == 2) {
                            Log.d("运算过程:",result + "-" + Float.parseFloat(str));
                            result = result - Float.parseFloat(str);
                            Log.d("result:", String.valueOf(result));
                        } else if (flag == 3) {
                            Log.d("运算过程:",result + "*" + Float.parseFloat(str));
                            result = result * Float.parseFloat(str);
                            Log.d("result:", String.valueOf(result));
                        } else if (flag == 4) {
                            Log.d("运算过程:",result + "/" + Float.parseFloat(str));
                            result = (Float) (result / Float.parseFloat(str));
                            Log.d("result:", String.valueOf(result));
                        }
                    }
                    flag = id;
                }
                str1 = "";
                str2 = "";
            }
        });
    }
}

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.tarena.day09_alertdialog; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private AlertDialog dialog1; private AlertDialog dialog2; private AlertDialog dialog3; private AlertDialog dialog4; private AlertDialog dialog5; private ProgressDialog dialog6; private TimePickerDialog dialog7; private DatePickerDialog dialog8; private EditText etUserName, etPassword; private boolean isFlag; private void initTimePickerDialog(){ dialog7 = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this, "Time:"+hourOfDay+":"+minute, Toast.LENGTH_LONG).show(); } }, 0, 0, false); } private void initDatePickerDialog(){ dialog8 = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this, "Date:"+year+":"+(monthOfYear+1)+":"+dayOfMonth, Toast.LENGTH_LONG).show(); } }, 2015, Calendar.SEPTEMBER, 17); } private void initAlertDialog1() { Builder builder = new Builder(this); // AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); // 添加显示信息 builder.setMessage("最简单对话框"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 创建AlertDialog对象 dialog1 = builder.create(); } private void initAlertDialog2() { Builder builder = new Builder(this); builder.setTitle("提示"); // 添加显示信息 builder.setMessage("最简单对话框"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 设置确定按钮 builder.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show(); } }); // 设置取消按钮 builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog2.dismiss(); } }); // 创建AlertDialog对象 dialog2 = builder.create(); } private void initAlertDialog3() { Builder builder = new Builder(this); builder.setTitle("提示"); // 设置控件方法 builder.setView(loadView()); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 设置确定按钮 builder.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String username = etUserName.getText().toString(); String password = etPassword.getText().toString(); Toast.makeText( MainActivity.this, "UserName:" + username + ",Password:" + password, Toast.LENGTH_SHORT).show(); } }); // 设置取消按钮 builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog3.dismiss(); } }); // 创建AlertDialog对象 dialog3 = builder.create(); } private void initAlertDialog4() { Builder builder = new Builder(this); builder.setTitle("提示"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); builder.setSingleChoiceItems(new String[] { "item1", "item2", "item3", "item4" }, 0, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: Toast.makeText(MainActivity.this, "item1", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MainActivity.this, "item2", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MainActivity.this, "item3", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MainActivity.this, "item4", Toast.LENGTH_SHORT).show(); break; default: break; } } }); // 创建AlertDialog对象 dialog4 = builder.create(); } private void initAlertDialog5(){ Builder builder = new Builder(this); builder.setTitle("提示"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); builder.setMultiChoiceItems(new String[]{"item1","item2","item3","item4"}, new boolean[]{true,false,true,false}, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this, "Which:"+which+",IsChechked:"+isChecked, Toast.LENGTH_SHORT).show(); } }); dialog5 = builder.create(); } /** * 加载布局 * * @return */ private View loadView() { LayoutInflater inflater = LayoutInflater.from(this); // LayoutInflater inflater = (LayoutInflater) // getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.login, null); etUserName = (EditText) view.findViewById(R.id.et_username); etPassword = (EditText) view.findViewById(R.id.et_password); return view; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initAlertDialog1(); initAlertDialog2(); initAlertDialog3(); initAlertDialog4(); initAlertDialog5(); initTimePickerDialog(); initDatePickerDialog(); addListener(); } private void addListener() { this.findViewById(R.id.button1).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog1.show(); } }); this.findViewById(R.id.button2).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog2.show(); } }); this.findViewById(R.id.button3).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog3.show(); } }); this.findViewById(R.id.button4).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog4.show(); } }); this.findViewById(R.id.button5).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog5.show(); } }); this.findViewById(R.id.button6).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { isFlag = true; dialog6 = new ProgressDialog(MainActivity.this); dialog6.setTitle("提示"); dialog6.setIcon(R.drawable.ic_launcher); dialog6.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog6.setProgress(100); dialog6.setCancelable(false); dialog6.setButton("完成", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { isFlag = false; dialog6.dismiss(); } }); dialog6.show(); new Thread(){ public void run() { int number = 0; while (isFlag) { dialog6.setProgress(number); number++; if(number > 100){ isFlag = false; dialog6.cancel(); } try { Thread.sleep(80); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }.start(); } }); this.findViewById(R.id.button7).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog7.show(); } }); this.findViewById(R.id.button8).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog8.show(); } }); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值