亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

android打造的最簡單計算器界面

系統(tǒng) 1818 0
先看圖:
android打造的最簡單計算器界面
這里主要是鍛煉一下TableLayout布局,注意其中的android:stretchColumns="0,1,2,3"屬性,該屬性可以控制每列的寬的權重,類似weight,由于這里4列都是“平等的”,所以是“0,1,2,3”,全部布局文件如下:
    
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <EditText 
    android:id="@+id/result" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content"
	android:text=""
	android:singleLine="true"
	android:numeric="decimal"
	android:focusable="false"
	android:digits="1234567890."
	android:cursorVisible="false"
	/>
	<TableLayout 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content"
	android:stretchColumns="0,1,2,3">
		<TableRow>
			<Button android:id="@+id/btn_1" android:text="1"/>
			<Button android:id="@+id/btn_2" android:text="2" />
			<Button android:id="@+id/btn_3" android:text="3" />
			<Button android:id="@+id/btn_add" android:text="+" />
		</TableRow>
		<TableRow>
			<Button android:id="@+id/btn_4" android:text="4" />
			<Button android:id="@+id/btn_5" android:text="5" />
			<Button android:id="@+id/btn_6" android:text="6" />
			<Button android:id="@+id/btn_sub" android:text="-" />
		</TableRow>
		<TableRow>
			<Button android:id="@+id/btn_7" android:text="7" />
			<Button android:id="@+id/btn_8" android:text="8" />
			<Button android:id="@+id/btn_9" android:text="9" />
			<Button android:id="@+id/btn_mul" android:text="*" />
		</TableRow>
		<TableRow>
			<Button android:id="@+id/btn_dot" android:text="." />
			<Button android:id="@+id/btn_0" android:text="0" />
			<Button android:id="@+id/btn_eq" android:text="=" />
			<Button android:id="@+id/btn_div" android:text="/" />
		</TableRow>
	</TableLayout>
	<Button 
	android:id="@+id/btn_clear" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content"
	android:text="clear"
	/>
</LinearLayout>

  

java源碼如下,這代碼我承認寫的不好,主要是數(shù)字鍵button沒有使用數(shù)組的形式,這樣很拖沓,將就著看吧!
    
package com.wt.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class App extends Activity {
	
	EditText editText;
	StringBuffer text;
	String number_1="",number_2="";
	boolean clicked=false;
	byte style=0;
	Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0;
	Button btn_add,btn_sub,btn_mul,btn_div,btn_eq,btn_dot,btn_clear;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text=new StringBuffer();
        editText=(EditText)findViewById(R.id.result);
        //editText.setFocusable(false);
        btn_1=(Button) findViewById(R.id.btn_1);
        btn_1.setOnClickListener(new Button.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
				text.append("1");
				editText.setText(text.toString());
				
			}
        	
        });
        btn_2=(Button) findViewById(R.id.btn_2);
        btn_2.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("2");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_3=(Button) findViewById(R.id.btn_3);
        btn_3.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("3");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_4=(Button) findViewById(R.id.btn_4);
        btn_4.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("4");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_5=(Button) findViewById(R.id.btn_5);
        btn_5.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("5");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_6=(Button) findViewById(R.id.btn_6);
        btn_6.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("6");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_7=(Button) findViewById(R.id.btn_7);
        btn_7.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("7");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_8=(Button) findViewById(R.id.btn_8);
        btn_8.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("8");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_9=(Button) findViewById(R.id.btn_9);
        btn_9.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("9");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_0=(Button) findViewById(R.id.btn_0);
        btn_0.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("0");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_dot=(Button) findViewById(R.id.btn_dot);
        btn_dot.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		if(editText.getText().toString().indexOf(".")==-1){//已經(jīng)有.了
        			if(text.length()==0){
        				text.append("0.");
        			}else{
        				text.append(".");
        			}
        			editText.setText(text.toString());
        		}
        	}
        	
        });
        
        btn_add=(Button) findViewById(R.id.btn_add);
        btn_add.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_1=editText.getText().toString();
        		style=1;
        		clicked=true;
        	}
        	
        });
        btn_sub=(Button) findViewById(R.id.btn_sub);
        btn_sub.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_1=editText.getText().toString();
        		style=2;
        		clicked=true;
        	}
        	
        });
        btn_mul=(Button) findViewById(R.id.btn_mul);
        btn_mul.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_1=editText.getText().toString();
        		style=3;
        		clicked=true;
        	}
        	
        });
        btn_div=(Button) findViewById(R.id.btn_div);
        btn_div.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_1=editText.getText().toString();
        		style=4;
        		clicked=true;
        	}
        	
        });
        btn_eq=(Button) findViewById(R.id.btn_eq);
        btn_eq.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_2=editText.getText().toString();
        		if(number_1.equals("")&&number_2.equals("")){
        			return;
        		}
        		Double d1=Double.valueOf(number_1);
        		Double d2=Double.valueOf(number_2);
        		Double r=0.0;;
        		switch (style) {
				case 1:
					r=d1+d2;
					break;
				case 2:
					r=d1-d2;
					break;
				case 3:
					r=d1*d2;
					break;
				case 4:
					r=d1/d2;
					break;
				}
        		editText.setText(r.toString());
        	}
        	
        });
        btn_clear=(Button) findViewById(R.id.btn_clear);
        btn_clear.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		editText.setText("");
        		number_1="";
        		number_2="";
        		style=0;
        		clicked=false;
        		text.setLength(0);
        	}
        	
        });
    }
    
}

  


Android——Tabel布局,計算器的實現(xiàn)
http://blog.csdn.net/zhanghaidang/archive/2011/04/27/6366771.aspx

android打造的最簡單計算器界面


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 私人免费影院入口 | 亚洲国产精品久久久久网站 | 涩涩视频免费观看 | 午夜福利毛片 | 2019天天干天天操 | 亚洲视频一区在线播放 | 狠狠色丁香婷婷久久综合考虑 | 亚洲欧美国产视频 | 香蕉久久国产 | 国产性一交一乱一伦一色一情 | 综合在线视频精品专区 | 亚洲欧美成人网 | 91欧美| 九九精品免视看国产成人 | 成人午夜爱爱爱爱爱 | 波多野结衣一区二区三区高清在线 | 日本老年人精品久久中文字幕 | 99国产超薄丝袜足j在线播放 | 伊人婷婷色香五月综合缴缴情 | 日本高清中文字幕一区二区三区 | 9久热这里只有精品免费 | 久久99精品久久久 | 99在线影院 | 欧美精欧美乱码一二三四区 | www.777奇米| 久久精品看片 | 亚洲视频在线观看免费视频 | 亚洲精品99久久一区二区三区 | 91精品福利视频 | 男女www | 国产成人夜色91 | 日本一级毛片毛片一级毛片 | 99精品在免费线视频 | 中文字幕日本一区久久 | 欧美一区二区三区在线 | 无遮挡又黄又爽又色1000部 | 四虎国产精品永久地址49 | 日本特黄一级午夜剧场毛片 | 久久在线精品 | 欧美福利视频在线 | 最新九九精品 |