android 计算器(GridView实现)

GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示按钮、图片等内容,比如实现九宫格图、计算器等,用GridView是首选,也是最简单的。应用主要实现了简单的计算器功能。

代码下载地址:点击打开链接

1、首先为GridView设置适配器 ,在适配器里定义一个图片数组和一个字符串数组,图片数组用于按钮的背景图片,字符串数组用于控件数字显示。

final String[] text1={
					"1","2","3",
					"4","5","6",
					"7","8","9",
					"-","*","+",
					"/","clear","="
			   };
private Integer[] imgs = {
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
				
		};

2、获取自定义布局和控件间的间距
LayoutInflater inflater = LayoutInflater.from(context);  
			   convertView = inflater.inflate(R.layout.item, null);   
			   convertView.setPadding(5, 5, 5, 5);  //每格的间距  

布局为一个按钮和一个文本控件:item.xml

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button 
	  android:id="@+id/button" 
	  android:layout_width="100dp" 
	  android:layout_height="50dp" 
	  android:layout_gravity="center_horizontal" 
  /> 
  <!--  
	 <TextView 
	  android:id="@+id/text" 
	  
	  android:layout_width="wrap_content" 
	  android:layout_height="wrap_content" 
	  android:textSize="18sp" 
	  android:layout_marginLeft="15dip"
	  android:layout_marginTop="8dp" 
	  /> 
	  
	  -->
</RelativeLayout>


3、保存前三次点击的数据然后进行判断,如果是clear就重新输入,然后对第四次输入数据进行判断再执行相应操作,实现代码:

if(i==0){
							i++;							
							numfirst = text1[position];				//记录第一次点击数字	
							if(numfirst.equals("clear")){//如果等于clear 则重新来
								i=0;
								return;
							}
							Toast.makeText(getApplication(), numfirst+"", Toast.LENGTH_SHORT).show();
							return;
						}else if(i==1){
							i++;									
							numsecond =text1[position]; //记录第2次点击数字																			
							if(numsecond.equals("clear")){
								i=0;
								return;
							}							
							
							Toast.makeText(getApplication(), numsecond+"", Toast.LENGTH_SHORT).show();
							return;
						}else if(i == 2){
							i++;
							numthree= text1[position];//记录第三次数据
							
							if(numthree.equals("clear")){
								i=0;
								return;
							}
							Toast.makeText(getApplication(), numthree+"", Toast.LENGTH_SHORT).show();
							return;
						}else if(i == 3){
						     String numfour= text1[position];//获取第4次点击数据进行判断
						     if(numfour.equals("clear")){
								 i=0;
								 return;
							 }
						     if(numfour.equals("=")){
						    	//判断 + - * / 则执行对应操作
									if(numsecond.equals("+")){
										int a =Integer.parseInt(numfirst)+Integer.parseInt(numthree);
										text.setText("a="+a);
									}else if(numsecond.equals("-")){
										int a =Integer.parseInt(numfirst)-Integer.parseInt(numthree);
										text.setText("a="+a);
									}else if(numsecond.equals("*")){
										int a =Integer.parseInt(numfirst)*Integer.parseInt(numthree);
										text.setText("a="+a);
									}else if(numsecond.equals("/")){
										Float a = Float.parseFloat(numfirst)/Integer.parseInt(numthree);
										text.setText("a="+a);
									}
						     }
						     i=0;
						     return;
						}

完整代码:

package com.example.hui;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private GridView gridview;
	private TextView text;
	String numfirst;//第1次点击保存数据
	String numsecond;//第2次点击保存数据
	String numthree;//第3次点击保存数据
	int i=0;//
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		
		initUI();
		
	}
	private void initUI(){
		gridview = (GridView)findViewById(R.id.gridview);
		text = (TextView)findViewById(R.id.text);
		 //为GridView设置适配器 
		gridview.setAdapter(new MyAdapter(this)); 
	}
	
	class MyAdapter extends BaseAdapter{

		private Context context; 
		private Integer[] imgs = {
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
				
		};
		MyAdapter(Context context){ 
	        this.context = context; 
	    } 
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return imgs.length;
		}

		@Override
		public Object getItem(int item) {
			// TODO Auto-generated method stub
			return item;
		}

		@Override
		public long getItemId(int id) {
			// TODO Auto-generated method stub
			return id;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			   
			   final String[] text1={
					"1","2","3",
					"4","5","6",
					"7","8","9",
					"-","*","+",
					"/","clear","="
			   };
			   
			   LayoutInflater inflater = LayoutInflater.from(context);  
			   convertView = inflater.inflate(R.layout.item, null);   
			   convertView.setPadding(5, 5, 5, 5);  //每格的间距  
			   
			   Button button = (Button)convertView.findViewById(R.id.button);
			   button.setText(text1[position]);
			   			   
			   button.setOnClickListener(new OnClickListener() {
				
				   @Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if(i==0){
							i++;							
							numfirst = text1[position];				//记录第一次点击数字	
							if(numfirst.equals("clear")){//如果等于clear 则重新来
								i=0;
								return;
							}
							Toast.makeText(getApplication(), numfirst+"", Toast.LENGTH_SHORT).show();
							return;
						}else if(i==1){
							i++;									
							numsecond =text1[position]; //记录第2次点击数字																			
							if(numsecond.equals("clear")){
								i=0;
								return;
							}							
							
							Toast.makeText(getApplication(), numsecond+"", Toast.LENGTH_SHORT).show();
							return;
						}else if(i == 2){
							i++;
							numthree= text1[position];//记录第三次数据
							
							if(numthree.equals("clear")){
								i=0;
								return;
							}
							Toast.makeText(getApplication(), numthree+"", Toast.LENGTH_SHORT).show();
							return;
						}else if(i == 3){
						     String numfour= text1[position];//获取第4次点击数据进行判断
						     if(numfour.equals("clear")){
								 i=0;
								 return;
							 }
						     if(numfour.equals("=")){
						    	//判断 + - * / 则执行对应操作
									if(numsecond.equals("+")){
										int a =Integer.parseInt(numfirst)+Integer.parseInt(numthree);
										text.setText("a="+a);
									}else if(numsecond.equals("-")){
										int a =Integer.parseInt(numfirst)-Integer.parseInt(numthree);
										text.setText("a="+a);
									}else if(numsecond.equals("*")){
										int a =Integer.parseInt(numfirst)*Integer.parseInt(numthree);
										text.setText("a="+a);
									}else if(numsecond.equals("/")){
										Float a = Float.parseFloat(numfirst)/Integer.parseInt(numthree);
										text.setText("a="+a);
									}
						     }
						     i=0;
						     return;
						}
						
						
						
					}
			   });
			   
			   //button.setBackgroundResource(imgs[position]);
			  // TextView text = (TextView)convertView.findViewById(R.id.text);
			  // text.setText(text1[position]);
			    
			   return convertView;
		}
		
	}

}

main.xml布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
<!-- android:numColumns="3" 设置列数 -->
<TextView 
       android:id="@+id/text"
       android:layout_gravity="center_horizontal"
       android:textSize="30sp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

   <GridView 
	  android:id="@+id/gridview" 
	  android:layout_width="fill_parent" 
	  android:layout_height="wrap_content" 
	  android:numColumns="3"  
	  android:columnWidth="50dp" 
	  android:layout_marginTop="5dp" 
  /> 
   

</LinearLayout>
图片效果:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值