Android应用的界面编程

Android应用的界面编程

一、实验目的

  1. 了解界面编程和视图
  2. 掌握Android界面的几种布局方式
  3. 掌握常用的集中UI组件
    二、实验内容
  4. 设计一个计算器界面,如下图所示。
    在这里插入图片描述
  5. 创建一个项目,界面中包含5个按钮,用户点击不同按钮时显示不同的对话框,可参考下图所示,可以自定义各个对话框显示的内容,自定义图标。
    在这里插入图片描述
    三、实验步骤

(1)打开Android studio软件,File -> new -> new project,
按照提示next就可以。我们打开activity_main.xml文件的text视图,在该视图下,布局界面

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:rowCount="6"
    android:columnCount="4"
    android:id="@+id/root">
    tools:context="com.example.nuist__njupt.gridlayouttes.MainActivity">

    <!-- 定义一个横跨4列的文本框,
	并设置该文本框的前景色、背景色等属性  -->
    <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"/>
    <!-- 定义一个横跨4列的按钮 -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"/>
</GridLayout>

(2)在Main_activity.java中设计文本按钮,代码如下:
遇到问题:Alt+Enter基本可以解决。

public class MainActivity extends ActionBarActivity {

    GridLayout gridLayout;
    // 定义16个按钮的文本
    String[] chars = new String[]
            {
                    "7" , "8" , "9" , "÷",
                    "4" , "5" , "6" , "×",
                    "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);
        }
    }
}


(3)用模拟器运行得到如图所示界面
在这里插入图片描述
实验2可以参照我的上一篇博客,就是对话框的使用,博客链接如下:https://blog.csdn.net/nuist_NJUPT/article/details/104960613

如果此博客对你有帮助,点个赞再走吧,谢谢!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值