《移动项目实践》实验报告——初级控件

Posted Starzkg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《移动项目实践》实验报告——初级控件相关的知识,希望对你有一定的参考价值。

实验目的

1、掌握android屏幕显示与初级视图的相关知识;
2、掌握包括屏幕显示基础、简单布局的用法、简单控件的用法、简单图形的用法

实验内容

以windows上的计算器为例,程序界面如下图所示:

完成精简之后的Android版本的计算器效果

简单计算器的运算界面效果

实验过程(实验的设计思路、关键源代码等)

源代码:https://gitee.com/shentuzhigang/mini-project/tree/master/android-simple-calculator

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/title"
        android:textAlignment="center"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:textSize="20sp"
        android:padding="4sp"
        android:textColor="@color/black"
        android:background="@drawable/border"
        android:gravity="end|bottom" />

    <GridLayout
        android:id="@+id/keyboard"
        android:layout_width="match_parent"
        android:layout_height="560dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:columnCount="4"
        android:rowCount="5"
        android:useDefaultMargins="true">

        <Button
            android:id="@+id/button10"
            android:text="CE"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button11"
            android:text="÷"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button12"
            android:text="×"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button13"
            android:text="C"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button7"
            android:text="@string/_7"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button8"
            android:text="@string/_8"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button9"
            android:text="@string/_9"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button14"
            android:text="+"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button4"
            android:text="@string/_4"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button5"
            android:text="@string/_5"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button6"
            android:text="@string/_6"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button15"
            android:text="-"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button1"
            android:text="@string/_1"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button2"
            android:text="@string/_2"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button3"
            android:text="@string/_3"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button16"
            android:text=""
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button0"
            android:layout_row="4"
            android:layout_column="0"
            android:layout_columnSpan="2"
            android:text="@string/_0"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button17"
            android:text="."
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />

        <Button
            android:id="@+id/button18"
            android:text="="
            android:layout_columnWeight="1"
            android:layout_rowWeight="1" />
    </GridLayout>


</LinearLayout>

MainActivity

package io.shentuzhigang.demo.calculator

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    private lateinit var textView: TextView

    private lateinit var bt_num0: Button
    private lateinit var bt_num1: Button
    private lateinit var bt_num2: Button
    private lateinit var bt_num3: Button
    private lateinit var bt_num4: Button
    private lateinit var bt_num5: Button
    private lateinit var bt_num6: Button
    private lateinit var bt_num7: Button
    private lateinit var bt_num8: Button
    private lateinit var bt_num9: Button
    private lateinit var bt_ce: Button
    private lateinit var bt_div: Button
    private lateinit var bt_mult: Button
    private lateinit var bt_minus: Button
    private lateinit var bt_plus: Button
    private lateinit var bt_equal: Button
    private lateinit var bt_dot: Button
    private lateinit var bt_clear: Button
    private lateinit var bt_sqrt: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        this.textView = findViewById(R.id.textView)
        this.bt_num0 = findViewById(R.id.button0)
        this.bt_num0.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num1 = findViewById(R.id.button1)
        this.bt_num1.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num2 = findViewById(R.id.button2)
        this.bt_num2.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num3 = findViewById(R.id.button3)
        this.bt_num3.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num4 = findViewById(R.id.button4)
        this.bt_num4.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num5 = findViewById(R.id.button5)
        this.bt_num5.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num6 = findViewById(R.id.button6)
        this.bt_num6.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num7 = findViewById(R.id.button7)
        this.bt_num7.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num8 = findViewById(R.id.button8)
        this.bt_num8.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_num9 = findViewById(R.id.button9)
        this.bt_num9.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            }
        })
        this.bt_ce = findViewById(R.id.button10)
        this.bt_ce.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {

            }
        })
        this.bt_div = findViewById(R.id.button11)
        this.bt_div.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                textView.text = textView.text.toString() + (v as Button).text
            以上是关于《移动项目实践》实验报告——初级控件的主要内容,如果未能解决你的问题,请参考以下文章

《移动项目实践》实验报告——Android中级控件

《移动项目实践》实验报告——Android高级控件

《移动项目实践》实验报告——Android组合控件

《移动项目实践》实验报告——Android组合控件

《移动项目实践》实验报告——Android设备操作

《移动项目实践》实验报告——Android设备操作