Android常用控件二
来源:互联网 发布:淘宝联盟是不是真的 编辑:程序博客网 时间:2024/05/17 08:58
先来一张思维导图
今天给大家分享一个简易计算器,出来效果图如下图:确定按钮下面是有一个文本框的,我没设置填充色所以看不出来
首先我们去xml把样式做出来,并给除确定按钮外所有的控件设置一个id, 给按钮写一个onClick事件,源代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入数字" android:textSize="20sp" android:id="@+id/et_deng_name1" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/rg_deng_d" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:textSize="30dp" android:id="@+id/rb_deng_jia" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="35dp" android:id="@+id/rb_deng_jian" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" android:textSize="30dp" android:id="@+id/rb_deng_chenga" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:textSize="30dp" android:id="@+id/rb_deng_chu" /> </RadioGroup> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入数字" android:textSize="20sp" android:id="@+id/et_deng_name2" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/btn_up1" android:id="@+id/bt_deng_queding" android:onClick="count" /> <TextView android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ff0000" android:gravity="center" android:id="@+id/et_deng_zhi" android:background="#55000000" /></LinearLayout>
然后写java代码,在设置的onClick事件中,获取二个输入框的值,并通过单选按钮组判断所选中的运算符,计算结果,再将运算结果放入文本框中
package com.example.myapp;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;/** * Created by Administrator on 2017/7/17 0017. */public class DendActivty extends AppCompatActivity{ private EditText et_deng_name1; private EditText et_deng_name2; private RadioGroup rg_deng_d; private TextView et_deng_zhi; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activty_deng); et_deng_zhi = (TextView) findViewById(R.id.et_deng_zhi); et_deng_name1 = (EditText) findViewById(R.id.et_deng_name1); et_deng_name2 = (EditText) findViewById(R.id.et_deng_name2); rg_deng_d = (RadioGroup) findViewById(R.id.rg_deng_d); } public void count(View v){ //获取输入框的值 double n= Double.parseDouble(et_deng_name1.getText().toString()); double m= Double.parseDouble(et_deng_name2.getText().toString()); //获得单选按钮 int id= rg_deng_d.getCheckedRadioButtonId(); RadioButton bu= (RadioButton) findViewById(id); String zhi= bu.getText().toString(); if(zhi.equals("+")){ n+=m;
//将计算结果放入文本中
et_deng_zhi.setText(""+n); } if(zhi.equals("-")){ n-=m; et_deng_zhi.setText(""+n); } if(zhi.equals("*")){ n*=m; et_deng_zhi.setText(""+n); } if(zhi.equals("/")){ n/=m; et_deng_zhi.setText(""+n); } }}
阅读全文
0 0
- Android常用控件(二)
- android常用控件(二)
- Android常用控件二
- Android常用控件二
- android常用控件二 gallery
- Android常用控件(二)
- Android 常用控件(二)
- android 常用控件归类(二)
- android常用控件(二)
- Android常用控件(二)【ProgressBar、ListView】
- Android常用复杂控件使用(二)--ListView
- Android常用自定义控件(二)
- Android常用控件之ListView(二)
- Android—常用控件(二)按钮
- Android常用控件二之编辑文本的控件
- Android学习笔记:<二>03.Android当中的常用控件
- MFC常用控件二
- 常用控件(二) : RecyclerView
- c++虚函数经典例子演示
- 接着上文MSQL 查的基础命令
- C#连接MySql报错:KeyNotFoundException, 给定关键字不在字典中
- php 将图片文件转成base64编码的方法
- Java知识:异常到底有啥用?
- Android常用控件二
- 明日之子
- vue瀑布流组件滑动加载更多
- Jmeter使用
- Let the Balloon Rise(map+前向迭代器)
- B
- 浅析 Linux 初始化 init 系统,第 3 部分: Systemd
- Android Killer 反编译出现问题 正在反编译APK源码,请稍等 解决
- laravel+vue2.0终于搞定了!