学安卓布局,做了一个简单的计算器。如图
只能实现简单的加减乘除。
上代码
<Button
android:id="@+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/b4"
android:layout_alignBottom="@+id/b4"
android:layout_alignLeft="@+id/b2"
android:text="5" />
<Button
android:id="@+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/b5"
android:layout_alignBottom="@+id/b5"
android:layout_toRightOf="@+id/b5"
android:text="6" />
<Button
android:id="@+id/b8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/b7"
android:layout_alignBottom="@+id/b7"
android:layout_toLeftOf="@+id/b6"
android:text="8" />
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/b1"
android:layout_below="@+id/b1"
android:text="4" />
<Button
android:id="@+id/b7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/b4"
android:layout_below="@+id/b4"
android:text="7" />
<Button
android:id="@+id/b9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/b8"
android:layout_alignBottom="@+id/b8"
android:layout_alignLeft="@+id/b6"
android:text="9" />
<Button
android:id="@+id/bt6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/b0"
android:layout_below="@+id/b0"
android:text="." />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/b6"
android:layout_below="@+id/et"
android:layout_marginTop="24dp"
android:text="3" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/b3"
android:layout_alignBottom="@+id/b3"
android:layout_toRightOf="@+id/b1"
android:text="2" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/b2"
android:layout_alignBottom="@+id/b2"
android:layout_toLeftOf="@+id/button1"
android:text="1" />
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/bt2"
android:layout_alignBottom="@+id/bt2"
android:layout_toLeftOf="@+id/b9"
android:text="-" />
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/bt1"
android:layout_alignBottom="@+id/bt1"
android:layout_toRightOf="@+id/bt1"
android:text="*" />
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/b7"
android:layout_below="@+id/b7"
android:text="+" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/et"
android:text=" "
android:textSize="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"/>
用相对布局做的 也没有设置圆角什么的
看MainActivity
package com.example.jsq_yd0216;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText editText;
Button bt01,bt02,bt03,bt04,bt05,bt06,bt07,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
String opt = “+”;
double n1 = 0.0, n2 = 0.0;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
OnClickListener lisenter = new OnClickListener() {
//设置公共监听器,但是还是有点乱
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText = (EditText)findViewById(R.id.et);
textView = (TextView) findViewById(R.id.tv);
String s = editText.getText().toString();//获取字符串
Button btn =(Button)v;
switch(btn.getId())
{
case R.id.b1://1
{
String str = editText.getText().toString();
editText.setText(str + 1);
str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.bt2://+
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "+";
textView.setText(n1 + opt);
editText.setText("");
break;
}
case R.id.b2://2
{
String str = editText.getText().toString();
editText.setText(str + 2);
str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.bt5://操作符=
{
if(opt == "+")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 + n2) + "");
}
else if(opt == "-")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 - n2) + "");
}
else if(opt == "*")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 * n2) + "");
}
else if(opt == "/")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
if(n2 == 0)
{
editText.setText("");
textView.setText("除数不能为0");
break;
}
else
{
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 / n2) + "");
}
}
break;
}
case R.id.b3://3
{
editText.setText(editText.getText().toString() + 3);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.b4://4
{
editText.setText(editText.getText().toString() + 4);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.b5://5
{
editText.setText(editText.getText().toString() + 5);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.b6://6
{
editText.setText(editText.getText().toString() + 6);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.b7://7
{
editText.setText(editText.getText().toString() + 7);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.b8://8
{
editText.setText(editText.getText().toString() + 8);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.b9://9
{
editText.setText(editText.getText().toString() + 9);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.b0://0
{
textView.setText(n1 + opt + 10);
editText.setText(editText.getText().toString() + 0);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.bt6://.
{
String str = editText.getText().toString();
if(str.indexOf(".") != -1)
//判断字符串中是否已经包含了小数点,如果有就什么也不做
{
}
else //如果没有小数点
{
if(str.equals("0"))//如果开始为0,
editText.setText(("0" + ".").toString());
else if(str.equals(""))//如果初时显示为空,就什么也不做
{
}
else
editText.setText(str + ".");
}
break;
}
case R.id.bt4://操作符 /
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "/";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.bt3://操作符*
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "*";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.bt1://操作符-
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "-";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.bt7://CE
{
String str =editText.getText().toString();
if(str.length() > 0)
editText.setText("");
break;
}
}
}};
bt01.setOnClickListener(lisenter);
bt02.setOnClickListener(lisenter);
bt03.setOnClickListener(lisenter);
bt04.setOnClickListener(lisenter);
bt05.setOnClickListener(lisenter);
bt06.setOnClickListener(lisenter);
bt07.setOnClickListener(lisenter);
b0.setOnClickListener(lisenter);
b1.setOnClickListener(lisenter);
b2.setOnClickListener(lisenter);
b3.setOnClickListener(lisenter);
b4.setOnClickListener(lisenter);
b5.setOnClickListener(lisenter);
b6.setOnClickListener(lisenter);
b7.setOnClickListener(lisenter);
b8.setOnClickListener(lisenter);
b9.setOnClickListener(lisenter);
}
public void init(){
bt01= (Button) findViewById(R.id.bt1);
bt02= (Button) findViewById(R.id.bt2);
bt03= (Button) findViewById(R.id.bt3);
bt04= (Button) findViewById(R.id.bt4);
bt05= (Button) findViewById(R.id.bt5);
bt06= (Button) findViewById(R.id.bt6);
bt07= (Button) findViewById(R.id.bt7);
b0= (Button) findViewById(R.id.b0);
b1= (Button) findViewById(R.id.b1);
b2= (Button) findViewById(R.id.b2);
b3= (Button) findViewById(R.id.b3);
b4= (Button) findViewById(R.id.b4);
b5= (Button) findViewById(R.id.b5);
b6= (Button) findViewById(R.id.b6);
b7= (Button) findViewById(R.id.b7);
b8= (Button) findViewById(R.id.b8);
b9= (Button) findViewById(R.id.b9);
editText= (EditText) findViewById(R.id.et);
}
}
总体就是这样子了。