本期的主角是CheckBox,主要是提供一个或多个选项让使用者进行核选
首先我们可以在Design利用拖曳的方式建置CheckBox
点击CheckBox可於右侧更改id、Text、width、height、textSize等属性
本次的范例使用LinearLayout建置,若还不会的捧油,可以去看我第11天的发文当中有介绍
完成後的<activity_main.xml>如下
<?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"
android:id="@+id/LinearLayout1">
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您要的餐点"
android:textSize="20sp" />
<CheckBox
android:id="@+id/ham"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="汉堡"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/fre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="薯条"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/cok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="可乐"
android:textSize="20sp"/>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"/>
</LinearLayout>
画面的部分长这样
接着我们来到java进行编译
package com.example.checkbox;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView txt1;
private CheckBox ham,fre,cok;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = findViewById(R.id.txt1);
ham = findViewById(R.id.ham);
fre = findViewById(R.id.fre);
cok = findViewById(R.id.cok);
ham.setOnCheckedChangeListener(myListener);
fre.setOnCheckedChangeListener(myListener);
cok.setOnCheckedChangeListener(myListener);
}
private CheckBox.OnCheckedChangeListener myListener =new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int n = 0;
if (ham.isChecked()){
n = n + 100;
}
if (fre.isChecked()){
n = n + 50;
}
if (cok.isChecked()){
n = n + 50;
}
txt1.setText("总金额为:" + n +"元");
}
};
}
上方程序当CheckBox有选取时,下方TextView会加总金额并显示
吃速食汉堡、薯条、饮料都不可或缺才对
<<: ASP.NET MVC 从入门到放弃(Day29)-MVC 实作一个web api
>>: 爬虫怎麽爬 从零开始的爬虫自学 DAY20 python网路爬虫开爬-3抓取整页标题
除了上述的应用,目前因人脸辨识技术的持续进步,使用人脸侦测时可以将五官抓出,来分析脸上五官的点,利用...
GitHub Repo https://github.com/b2etw/Spring-Kotlin...
【前言】 嗨嗨大家好,今天的主题延续昨天的检测是否已经安装插件後,紧接着而来的是 MetaMask...
影片在这里 分类:选股 型态 重点整理 金三角交叉:5 日均线先向上突破 10 日均线後再突破 20...
今天我们一样要来做练习,那我们就开始吧。 题目 让使用者输入一整数,求此整数以下(包含此数)的质数和...