Android案例(1)——一个简单的计算器
2015-07-05 14:08
483 查看
1. 设置文本框样式:
第一步:通过在drawble上创建一个名叫while_bg的Android XML File的文件,选择的是sharp元素。设置该背景图:
radius:四个角上的圆角半径。
gradient:设置背景图
stroke:设置边框
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <!-- <gradient android:startColor="#ff3344" android:endColor="#556633" /> --> <stroke android:width="1dp" android:color="#000000" /> <solid android:color="#ffffff"/> </shape>
第二步:那为了让整个页面的背景色是黑色的,可以在AndroidManifest.xml文件中去设置当前Activity:Theme.Black.NoTitleBar底是黑色的且没有标题栏。
<activity android:name="com.example.calculator.MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
第三步:设置EditText控件是不可编辑的,文字靠右显示的:
<EditText android:layout_width="fill_parent" android:layout_height="60dp" android:id="@+id/et_input" android:background="@drawable/white_bg" android:editable="false" android:gravity="right" />
第四步:添加按钮区域:
<?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" >
<EditText android:layout_width="fill_parent" android:layout_height="60dp" android:id="@+id/et_input" android:background="@drawable/white_bg" android:editable="false" android:gravity="right" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="@string/C"
android:textSize="15sp"
android:gravity="right"
android:id="@+id/btn_clear"
/>
<Button
android:id="@+id/btn_del"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="@string/DEL"
android:gravity="right"
android:textSize="15sp" />
<Button
android:id="@+id/btn_chu"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="@string/haha"
android:gravity="right"
android:textSize="15sp" />
<Button
android:id="@+id/btn_cheng"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="@string/cheng"
android:gravity="right"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="7"
android:textSize="15sp"
android:gravity="right"
android:id="@+id/btn_7"
/>
<Button
android:id="@+id/btn_8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="8"
android:gravity="right"
android:textSize="15sp" />
<Button
android:id="@+id/btn_9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="9"
android:gravity="right"
android:textSize="15sp" />
<Button
android:id="@+id/btn_jian"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="-"
android:gravity="right"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="4"
android:textSize="15sp"
android:gravity="right"
android:id="@+id/btn_4"
/>
<Button
android:id="@+id/btn_5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="5"
android:gravity="right"
android:textSize="15sp" />
<Button
android:id="@+id/btn_6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="6"
android:gravity="right"
android:textSize="15sp" />
<Button
android:id="@+id/btn_jia"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="+"
android:gravity="right"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="1"
android:textSize="15sp"
android:gravity="right"
android:id="@+id/btn_1"
/>
<Button
android:id="@+id/btn_2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="2"
android:gravity="right"
android:textSize="15sp" />
<Button
android:id="@+id/btn_3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="3"
android:gravity="right"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_0"
android:layout_width="140dp"
android:layout_height="60dp"
android:text="0"
android:gravity="right"
android:textSize="15sp" />
<Button
android:id="@+id/btn_dian"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="."
android:gravity="right"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_dengyu"
android:layout_width="60dp"
android:layout_height="130dp"
android:layout_marginLeft="20dp"
android:text="="
android:gravity="right"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
第五步:修改按钮样式:
步骤一:
首先需要在drawable文件中创建一堆sharp元素的图像,然后再创建一堆selector元素的文件,举例如下:
在gray.xml中:代表灰色
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="#f0f0f0"/> </shape>在white_selector.xml中:第一个item代表状态是按下时,按钮颜色变成灰色,第二个item代表默认状态下是白色的。
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/gray" android:state_pressed="true"/> <item android:drawable="@drawable/white_bg"/> </selector>步骤二:
在Button按钮的属性中添加:android:background="@drawable/white_selector"
<pre name="code" class="java"> <Button android:id="@+id/btn_3" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginLeft="20dp" android:background="@drawable/white_selector" android:text="3" android:gravity="right" android:textSize="15sp" />
步骤三:
给Button按钮设置paddingRight属性,设置内边距。
android:paddingRight="10dp" android:paddingTop="10dp"
至此页面布局就结束了,看看效果图:
当点击时,我们这个按钮变化颜色有点恶心,哈哈:
第六步:实例化控件:
用接口的方式实现点击事件响应:
package com.example.calculator; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.app.Activity; public class MainActivity extends Activity implements OnClickListener{ Button bt_0; // 0数字按钮 Button bt_1; // 1数字按钮 Button bt_2; // 2数字按钮 Button bt_3; // 3数字按钮 Button bt_4; // 4数字按钮 Button bt_5; // 5数字按钮 Button bt_6; // 6数字按钮 Button bt_7; // 7数字按钮 Button bt_8; // 8数字按钮 Button bt_9; // 9数字按钮 Button bt_point; // 小数点按钮 Button bt_jia; // +按钮 Button bt_jian; // -按钮 Button bt_cheng; // *按钮 Button bt_chu; // /按钮 Button bt_clear; // 清除按钮 Button bt_del; // 删除按钮 Button bt_denghao; // =按钮 EditText et_input; // 显示输入内容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_0 = (Button) findViewById(R.id.btn_0); bt_1 = (Button) findViewById(R.id.btn_1); bt_2 = (Button) findViewById(R.id.btn_2); bt_3 = (Button) findViewById(R.id.btn_3); bt_4 = (Button) findViewById(R.id.btn_4); bt_5 = (Button) findViewById(R.id.btn_5); bt_6 = (Button) findViewById(R.id.btn_6); bt_7 = (Button) findViewById(R.id.btn_7); bt_8 = (Button) findViewById(R.id.btn_8); bt_9 = (Button) findViewById(R.id.btn_9); bt_jia = (Button) findViewById(R.id.btn_jia); bt_jian = (Button) findViewById(R.id.btn_jian); bt_cheng = (Button) findViewById(R.id.btn_cheng); bt_chu = (Button) findViewById(R.id.btn_chu); bt_denghao = (Button) findViewById(R.id.btn_denghao); bt_clear = (Button) findViewById(R.id.btn_clear); bt_del = (Button) findViewById(R.id.btn_del); et_input = (EditText) findViewById(R.id.et_input); // 以上是实例化按钮 bt_0.setOnClickListener(this); bt_1.setOnClickListener(this); bt_2.setOnClickListener(this); bt_3.setOnClickListener(this); bt_4.setOnClickListener(this); bt_5.setOnClickListener(this); bt_6.setOnClickListener(this); bt_7.setOnClickListener(this); bt_8.setOnClickListener(this); bt_9.setOnClickListener(this); bt_jia.setOnClickListener(this); bt_jian.setOnClickListener(this); bt_cheng.setOnClickListener(this); bt_chu.setOnClickListener(this); bt_denghao.setOnClickListener(this); bt_clear.setOnClickListener(this); bt_del.setOnClickListener(this); // 以上是设置按钮的点击事件 } @Override public void onClick(View arg0) { // TODO Auto-generated method stub } }这一步真的是超无聊诶。
第七步:实现业务逻辑:然后就需要在onClick中判断当前点的是哪个按钮
需要注意的地方就是关于那个clear_flag的设置,它是用来标识结束上一次计算,清空上一次结果的。
下面是MainActivity.java的源代码:
package com.example.calculator; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.app.Activity; public class MainActivity extends Activity implements OnClickListener{ Button bt_0; // 0数字按钮 Button bt_1; // 1数字按钮 Button bt_2; // 2数字按钮 Button bt_3; // 3数字按钮 Button bt_4; // 4数字按钮 Button bt_5; // 5数字按钮 Button bt_6; // 6数字按钮 Button bt_7; // 7数字按钮 Button bt_8; // 8数字按钮 Button bt_9; // 9数字按钮 Button bt_point; // 小数点按钮 Button bt_jia; // +按钮 Button bt_jian; // -按钮 Button bt_cheng; // *按钮 Button bt_chu; // /按钮 Button bt_clear; // 清除按钮 Button bt_del; // 删除按钮 Button bt_denghao; // =按钮 EditText et_input; // 显示输入内容 boolean clear_flag; <span style="white-space:pre"> </span>// 清空标识 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_0 = (Button) findViewById(R.id.btn_0); bt_1 = (Button) findViewById(R.id.btn_1); bt_2 = (Button) findViewById(R.id.btn_2); bt_3 = (Button) findViewById(R.id.btn_3); bt_4 = (Button) findViewById(R.id.btn_4); bt_5 = (Button) findViewById(R.id.btn_5); bt_6 = (Button) findViewById(R.id.btn_6); bt_7 = (Button) findViewById(R.id.btn_7); bt_8 = (Button) findViewById(R.id.btn_8); bt_9 = (Button) findViewById(R.id.btn_9); bt_point = (Button) findViewById(R.id.btn_point); bt_jia = (Button) findViewById(R.id.btn_jia); bt_jian = (Button) findViewById(R.id.btn_jian); bt_cheng = (Button) findViewById(R.id.btn_cheng); bt_chu = (Button) findViewById(R.id.btn_chu); bt_denghao = (Button) findViewById(R.id.btn_denghao); bt_clear = (Button) findViewById(R.id.btn_clear); bt_del = (Button) findViewById(R.id.btn_del); et_input = (EditText) findViewById(R.id.et_input); // 以上是实例化按钮 bt_0.setOnClickListener(this); bt_1.setOnClickListener(this); bt_2.setOnClickListener(this); bt_3.setOnClickListener(this); bt_4.setOnClickListener(this); bt_5.setOnClickListener(this); bt_6.setOnClickListener(this); bt_7.setOnClickListener(this); bt_8.setOnClickListener(this); bt_9.setOnClickListener(this); bt_point.setOnClickListener(this); bt_jia.setOnClickListener(this); bt_jian.setOnClickListener(this); bt_cheng.setOnClickListener(this); bt_chu.setOnClickListener(this); bt_denghao.setOnClickListener(this); bt_clear.setOnClickListener(this); bt_del.setOnClickListener(this); // 以上是设置按钮的点击事件 } @Override public void onClick(View arg0) { // 然后就需要在onClick中判断当前点的是哪个按钮 String str = et_input.getText().toString(); switch(arg0.getId()){ case R.id.btn_0: case R.id.btn_1: case R.id.btn_2: case R.id.btn_3: case R.id.btn_4: case R.id.btn_5: case R.id.btn_6: case R.id.btn_7: case R.id.btn_8: case R.id.btn_9: case R.id.btn_point: if(clear_flag){ // 为的是下次运算时清空上次计算的内容。 clear_flag = false; str = ""; et_input.setText(""); } et_input.setText(str+((Button)arg0).getText()); // 添加内容到输入框中。 break; case R.id.btn_jia: case R.id.btn_jian: case R.id.btn_cheng: case R.id.btn_chu: if(clear_flag){ // 为的是下次运算时清空上次计算的内容。 clear_flag = false; str = ""; et_input.setText(""); } et_input.setText(str+" "+((Button)arg0).getText()+" "); // 添加内容到输入框中。 break; case R.id.btn_clear: clear_flag = false; // 还原 str = ""; et_input.setText(""); break; case R.id.btn_del: if(clear_flag){ // 为的是下次运算时清空上次计算的内容。 clear_flag = false; str = ""; et_input.setText(""); } else if(str!=null && !str.equals("")){ et_input.setText(str.substring(0,str.length()-1)); } break; case R.id.btn_denghao: getResult(); default: break; } }
相关文章推荐
- 【Android】利用Activity生命周期监听应用前后台切换
- android开发中经常看到@Override原来是这个意思
- 从源码解释Android事件分发机制
- Android之使用adb wireless进行调试
- Android NDK编程浅入深出之--Android.mk
- Android中ViewHolder模式开发的详细解释
- [Android]java.io.FileNotFoundException: open failed: EACCES (Permission denied)
- Android 中的 ViewPager+ Fragment
- Android进程与线程区别
- Android入门(8)——使用Intent实现页面跳转
- android FragmentActivity+FragmentTabHost+Fragment框架布局
- SWIG and Android
- [Android L]SEAndroid开放设备文件结点权限(读或写)方法(涵盖常用操作:sys/xxx、proc/xxx、SystemProperties)热门干货
- android动画之通过子线程来实现动画
- android动画之android:interpolator属性使用
- android4.0Caused by: android.os.NetworkOnMainThreadException问题
- Android IOS WebRTC 音视频开发总结(三二)-- WebRTC项目开发建议
- android事件系列-onTouch事件与手势操作
- android通过drawable资源实现常用的自定义效果
- Android EditText 一些属性设置