您的位置:首页 > 移动开发 > Android开发

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;
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: