Android實(shí)現(xiàn)簡單計(jì)算器界面
本文實(shí)例為大家分享了Android實(shí)現(xiàn)計(jì)算器界面的具體代碼,供大家參考,具體內(nèi)容如下
XML文件:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4" android:id="@+id/root"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:textSize="50sp" android:layout_marginLeft="2pt" android:layout_marginRight="2pt" android:padding="3pt" android:layout_gravity="right" android:background="#eee" android:textColor="#000" android:text="0" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除"/> </GridLayout>
MainActivity:
package learn.li.com.learnthree;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
GridLayout gridLayout;
String[] chars = new String[]{
"7","8","9","÷",
"4","5","6","x",
"1","2","3","-",
".","0","=","="
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout = (GridLayout)findViewById(R.id.root);
for(int i = 0;i < chars.length;i++){
Button bn = new Button(this);
bn.setText(chars[i]);
bn.setTextSize(40);
bn.setPadding(5,35,5,35);
GridLayout.Spec rowSpec = GridLayout.spec(i/4 + 2);
GridLayout.Spec columnSpec = GridLayout.spec(i%4);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec,columnSpec);
params.setGravity(Gravity.FILL);
gridLayout.addView(bn,params);
}
}
}
效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android List刪除重復(fù)數(shù)據(jù)
這篇文章主要介紹了Android List刪除重復(fù)數(shù)據(jù)的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06
Android使用listview實(shí)現(xiàn)分頁刷新(線程休眠模擬)
這篇文章主要為大家詳細(xì)介紹了Android使用listview實(shí)現(xiàn)分頁刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法
這篇文章主要介紹了Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法,實(shí)例分析了Android操作SQLite數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Android 自定義通用的loadingview實(shí)現(xiàn)代碼
本篇文章主要介紹了Android 自定義通用的loadingview實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
Android中使用ViewStub實(shí)現(xiàn)布局優(yōu)化
ViewStub是Android布局優(yōu)化中一個(gè)很不錯(cuò)的標(biāo)簽/控件,直接繼承自View。雖然Android開發(fā)人員基本上都聽說過,但是真正用的可能不多。今天我們就來詳細(xì)探討下ViewStub的使用2016-09-09
Android實(shí)現(xiàn)微信自動向附近的人打招呼(AccessibilityService)
這篇文章主要為大家詳細(xì)介紹了實(shí)現(xiàn)微信自動向附近的人打招呼,實(shí)現(xiàn)收到指定賬戶推送文章時(shí)自動進(jìn)入微信打開鏈接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

