安卓实验1 界面设计:控件和布局

实验题目:界面设计:控件和布局

  • 实验目的

1了解Android编程原理

2掌握基本布局管理器、控件的应用

3掌握控件的事件处理编程

  • 实验内容

编写一个小费计算器,界面如下。在Amount框中输入就餐费用,在15%标准小费率列下将按15%计算小费(Tip)的金额和应付总金额(Total),在18%定制小费比例下将按定制比例计算小费和应付总额。定制小费率可以通过拖动SeekBar进行修改。(自拟实验内容也可以,只要涉及到的是相同的知识点。比如可以是课堂上做过的练习题或者是自主学习时候的做过的练习题)

  • 程序设计思想(流程图,或算法思想或设计方案等)

本次实验设计一个小费计算器,需要使用到布局管理器和控件的应用。界面分为两部分,上半部分是输入框和SeekBar控件,下半部分是显示小费和总金额的文本框。实现的主要思路是:

UI设计:在activity_main.xml文件中,使用TextView显示“账单金额”、“小费金额”、“总金额”等标签,使用EditText允许用户输入账单金额,使用SeekBar提供滑块选择器以选择自定义小费百分比,使用RadioButton提供两个选项:15%和自定义百分比。

创建变量和实例化UI元素:在MainActivity类中,声明了一些变量来存储账单金额和自定义小费百分比,以及UI元素的引用,如tipTextView、totalTextView、amountEditText和customSeekBar等。在onCreate()方法中,使用findViewById()方法实例化这些UI元素,并将它们存储在对应的变量中。

实现监听器:为了在用户更改EditText、SeekBar或RadioButton时更新小费和总金额,需要实现相应的监听器。使用TextWatcher接口实现一个amountEditTextWatcher来监听EditText的更改,使用OnSeekBarChangeListener接口实现一个customSeekBarListener来监听SeekBar的更改,使用OnCheckedChangeListener接口实现一个监听器来监听RadioButton的更改。当用户更改EditText、SeekBar或RadioButton时,这些监听器将自动更新小费和总金额。

更新小费和总金额:为了更新小费和总金额,可以实现两个私有方法updateStandard()和updateCustom(),它们计算并更新小费和总金额的值,并将这些值显示在tipTextView和totalTextView中。

使用NumberFormat进行格式化:为了将小费和总金额格式化为货币值,可以使用NumberFormat类的静态方法getCurrencyInstance()创建一个货币格式对象,然后使用该对象的format()方法将double值格式化为货币值。

处理RadioButton选择:当用户选择15%的RadioButton时,将SeekBar的进度设置为15,并将EditText的文本设置为空。

处理SeekBar滑块选择:当用户拖动SeekBar时,根据SeekBar的进度计算自定义小费百分比,并更新小费和总金额。

  • 源代码(主要源代码)

MainActivity.java

package com.example.caculate;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.text.Editable;

import android.text.TextWatcher;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.SeekBar;

import android.widget.TextView;

import android.widget.CompoundButton;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

    private static final NumberFormat percentFormat = NumberFormat.getPercentInstance();

    private double billAmount = 0.0;

    private double customPercent = 0.18;

    private TextView tipTextView;

    private TextView totalTextView;

    private EditText amountEditText;

    private SeekBar customSeekBar;

    private RadioButton fifteenPercentRadioButton;

    private RadioButton  customPercentRadioButton;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       tipTextView = findViewById(R.id.tipTextView);

        totalTextView = findViewById(R.id.totalTextView);

        amountEditText = findViewById(R.id.amountEditText);

        customSeekBar = findViewById(R.id.customSeekBar);

        fifteenPercentRadioButton=findViewById(R.id.fifteenPercentRadioButton);

        customPercentRadioButton=findViewById(R.id.customPercentRadioButton);

        amountEditText.addTextChangedListener(amountEditTextWatcher);

        customSeekBar.setOnSeekBarChangeListener(customSeekBarListener);

        fifteenPercentRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked) {

                    customSeekBar.setProgress(15);

                    amountEditText.setText(""); // 将amountEditText清空

                }

            }

        });

    }

    private void updateStandard() {

        double tip = billAmount * 0.15;

        double total = billAmount + tip;

        tipTextView.setText(currencyFormat.format(tip));

        totalTextView.setText(currencyFormat.format(total));

    }

    private void updateCustom() {

//        percentTextView.setText(percentFormat.format(customPercent));

        double tip = billAmount * customPercent;

        double total = billAmount + tip;

        tipTextView.setText(currencyFormat.format(tip));

        totalTextView.setText(currencyFormat.format(total));

    }

    private final TextWatcher amountEditTextWatcher = new TextWatcher() {

        @Override

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override

        public void onTextChanged(CharSequence s, int start, int before, int count) {

            try {

                billAmount = Double.parseDouble(s.toString());

            } catch (NumberFormatException e) {

                billAmount = 0.0;

            }

            updateStandard();

            updateCustom();

        }

        @Override

        public void afterTextChanged(Editable s) {}

    };

    private final SeekBar.OnSeekBarChangeListener customSeekBarListener = new SeekBar.OnSeekBarChangeListener() {

        @Override

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            customPercent = progress / 100.0;

            updateCustom();

        }

        @Override

        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override

        public void onStopTrackingTouch(SeekBar seekBar) {}

    };

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:padding="16dp">

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/bill_amount_label" />

    <EditText

        android:id="@+id/amountEditText"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:inputType="numberDecimal"

        android:textSize="24sp" />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="tip_percent_label" />

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <RadioGroup

            android:id="@+id/tipRadioGroup"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="vertical">

            <RadioButton

                android:id="@+id/fifteenPercentRadioButton"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="fifteen_percent_label"

                android:checked="true" />

            <RadioButton

                android:id="@+id/customPercentRadioButton"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="custom_percent_label" />

        </RadioGroup>

        <SeekBar

            android:id="@+id/customSeekBar"

            android:layout_width="10dp"

            android:layout_height="wrap_content"

            android:layout_weight="3"

             />

        <TextView

            android:id="@+id/customPercentTextView"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="custom_percent_label"

            android:visibility="gone" />

    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        android:padding="8dp">

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/tip_amount_label" />

        <TextView

            android:id="@+id/tipTextView"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:textSize="24sp" />

    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        android:padding="8dp">

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/total_amount_label" />

        <TextView

            android:id="@+id/totalTextView"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:textSize="24sp" />

    </LinearLayout>

</LinearLayout>

  • 测试与运行
  1. 在调试程序的过程中遇到什么问题,是如何解决的?

在调试过程中可能会遇到控件无法响应点击事件的问题,这时可以检查代码中是否正确设置了控件的点击事件监听器,并且确保监听器方法中实现了正确的逻辑。

  1. 测试数据及运行结果。(无测试数据的,直接记录运行结果)

输入餐费金额为100,选择fiften_percent_lable设置定制小费率为15%,则计算结果如下:

小费金额:15.00

应付总金额:115.00

输入餐费金额为100,选择custom_precent_label设置定制小费率为50%,则计算结果如下:

小费金额: 50.00

应付总金额:150.00

  • 总结

本次实验通过编写小费计算器程序,深入学习了Android中常用的布局管理器和控件,以及控件事件的处理方法。通过本次实验,我掌握了以下知识:

LinearLayout布局管理器的使用方法,包括垂直和水平方向的布局。

TextView和EditText控件的基本属性和使用方法。

RadioGroup、RadioButton和SeekBar控件的基本属性和使用方法,以及如何为它们添加事件监听器。

如何为控件添加事件监听器,并实现相应的逻辑,如计算小费和总金额等。

通过本次实验,我不仅掌握了Android程序开发的基础知识,还能够为后续的Android应用开发打下良好的基础。在未来的Android应用开发中,我可以更加熟练地运用布局管理器和控件,并实现更加复杂的交互逻辑和UI设计。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现仿动物连连看游戏界面的具体步骤如下: 1. 创建一个名为"LinearLayout"的布局文件,将其保存在适当的包名下。可以选择一个合适的包名作为LinearLayout类的名称。 2. 在LinearLayout布局文件中,使用垂直方向的LinearLayout作为根布局容器,并设置合适的宽度和高度。 3. 在根布局容器中,添加多个水平方向的LinearLayout作为行容器,每个行容器表示游戏界面的一行,设置合适的宽度和高度。 4. 在每个行容器中,添加多个ImageView作为格子,并设置合适的宽度和高度。每个ImageView表示一个动物图标,可以用来进行连连看的操作。 5. 在Java代码中,创建一个LinearLayout的实例,设置其属性和布局文件的关联,使其显示在界面上。 6. 从资源文件或者动态生成的数据中获取动物图标的图片资源,并将这些资源设置给ImageView,实现动物图标的显示。 7. 添加点击事件监听器,当ImageView被点击后,根据点击位置和规则进行连连看的操作。 8. 在连连看的操作中,可以使用适当的算法进行图标匹配和消除的判断,实现游戏规则的实时判断和更新。 9. 如果匹配成功,可以按照游戏规则的要求,实现图标的消除或者替换。 10. 如果匹配失败,则根据游戏规则的要求,实现图标的翻转或者其他操作。 11. 根据游戏的进度,可以添加计时器或者其他提示功能,提高游戏的趣味性和可玩性。 通过以上步骤,可以实现一个基本的仿动物连连看游戏界面,玩家可以在其中进行游戏操作和娱乐。具体的实现过程可能还需要根据个人需求进行适当的调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值