实验题目:界面设计:控件和布局
- 实验目的
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>
- 测试与运行
- 在调试程序的过程中遇到什么问题,是如何解决的?
在调试过程中可能会遇到控件无法响应点击事件的问题,这时可以检查代码中是否正确设置了控件的点击事件监听器,并且确保监听器方法中实现了正确的逻辑。
- 测试数据及运行结果。(无测试数据的,直接记录运行结果)
输入餐费金额为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控件的基本属性和使用方法,以及如何为它们添加事件监听器。
如何为控件添加事件监听器,并实现相应的逻辑,如计算小费和总金额等。文章来源:https://www.toymoban.com/news/detail-735729.html
通过本次实验,我不仅掌握了Android程序开发的基础知识,还能够为后续的Android应用开发打下良好的基础。在未来的Android应用开发中,我可以更加熟练地运用布局管理器和控件,并实现更加复杂的交互逻辑和UI设计。文章来源地址https://www.toymoban.com/news/detail-735729.html
到了这里,关于安卓实验1 界面设计:控件和布局的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!