用嵌套的LinearLayout,和OnClickListener做了个计算器的小实例
源码下载:http://download.csdn.net/detail/lc448986375/4574515
布局代码:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:text="简易计算器" tools:context=".MainActivity" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > > <EditText android:id="@+id/num1" android:width="80dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:text=" " tools:context=".MainActivity" /> <EditText android:id="@+id/num2" android:width="80dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/denghao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" = " /> <EditText android:id="@+id/num3" android:width="80dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/jia" android:width="80dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" + " /> <Button android:id="@+id/jian" android:width="80dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" - " /> <Button android:id="@+id/cheng" android:width="80dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" * " /> <Button android:id="@+id/chu" android:width="80dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" / " /> </LinearLayout> </LinearLayout>
MainActivity.java代码:
package com.activity;import com.dao.Calculate;import android.os.Bundle;import android.app.Activity;import android.view.Menu;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 { //第一组件 //第一个数字,第二个数字,第三个数字 private EditText num1Text,num2Text,num3Text; //显示符号的控件 private TextView text; //加减乘除四个按钮 private Button jia,jian,cheng,chu; private Calculate calculate = new Calculate(); private double num1,num2,num3; //查找到组件 private void findViews(){ num1Text = (EditText) findViewById(R.id.num1); num2Text = (EditText) findViewById(R.id.num2); num3Text = (EditText) findViewById(R.id.num3); text= (TextView) findViewById(R.id.text1); jia = (Button) findViewById(R.id.jia); jian = (Button) findViewById(R.id.jian); cheng = (Button) findViewById(R.id.cheng); chu = (Button) findViewById(R.id.chu); } //添加监听 private void setListeners(){ jia.setOnClickListener(new OnClickListener() { public void onClick(View v) { text.setText("+"); num1 = Double.parseDouble(num1Text.getText().toString()); num2 = Double.parseDouble(num2Text.getText().toString()); num3 = calculate.add(num1, num2); num3Text.setText(Double.toString(num3)); } }); jian.setOnClickListener( new OnClickListener() { public void onClick(View v) { text.setText("-"); num1 = Double.parseDouble(num1Text.getText().toString()); num2 = Double.parseDouble(num2Text.getText().toString()); num3 = calculate.sub(num1, num2); num3Text.setText(Double.toString(num3)); } } ); cheng.setOnClickListener( new OnClickListener() { public void onClick(View v) { text.setText("*"); num1 = Double.parseDouble(num1Text.getText().toString()); num2 = Double.parseDouble(num2Text.getText().toString()); num3 = calculate.mul(num1, num2); num3Text.setText(Double.toString(num3)); } } ); chu.setOnClickListener( new OnClickListener() { public void onClick(View v) { text.setText("/"); num1 = Double.parseDouble(num1Text.getText().toString()); num2 = Double.parseDouble(num2Text.getText().toString()); num3 = calculate.chu(num1, num2); num3Text.setText(Double.toString(num3)); } } ); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.findViews(); this.setListeners(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
分了一下层,下面是dao层的代码
Calculate.java
package com.dao;public class Calculate { //加 public double add(double num1,double num2){ return num1 + num2; } //减 public double sub(double num1,double num2){ return num1 - num2; } //乘 public double mul(double num1,double num2){ return num1 * num2; } //除 public double chu(double num1,double num2){ return num1 / num2; }}
下面是效果图
时间关系,写的不太好,仅供参考