Android實現(xiàn)簡單計算器界面
更新時間:2021年01月27日 11:00:52 作者:OliverkingLi
這篇文章主要為大家詳細介紹了Android實現(xiàn)簡單計算器界面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android實現(xiàn)計算器界面的具體代碼,供大家參考,具體內(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?Studio實現(xiàn)帶三角函數(shù)對數(shù)運算功能的高級計算器
這篇文章主要為大家詳細介紹了Android?Studio實現(xiàn)帶三角函數(shù)對數(shù)運算功能的高級計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Android 應(yīng)用的全屏和非全屏實現(xiàn)代碼
這篇文章主要介紹了Android 應(yīng)用的全屏和非全屏實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05

