Android实现简单计算器界面
程序员文章站
2022-06-28 07:58:52
本文实例为大家分享了android实现计算器界面的具体代码,供大家参考,具体内容如下xml文件:...
本文实例为大家分享了android实现计算器界面的具体代码,供大家参考,具体内容如下
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); } } }
效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。