在程序中如果需要用到很多判断时,大部分会改使用switch多条件判断控制。
switch(ex)
{
case a:
//上方小括号内常数值等於此条件a,则会执行此区块程序
break;
case b:
//上方小括号内常数值等於此条件b,则会执行此区块程序
break;
case c:
//上方小括号内常数值等於此条件c,则会执行此区块程序
break;
default :
假设上方条件皆不成立,则会执行此区块
}
我们利用Switch case的语法制作一个按下Button後累加文字输出於TextView
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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#FFFF0000"
android:gravity="center"
android:text="显示:" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="4" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
</LinearLayout>
</LinearLayout>
MainActivity.java档范例如下:
package com.example.button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button button1,button2,button3,button4,button5,button6;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button5 = (Button)findViewById(R.id.button5);
button6 = (Button)findViewById(R.id.button6);
textView = (TextView)findViewById(R.id.textView);
button1.setOnClickListener(this::onClick);
button2.setOnClickListener(this::onClick);
button3.setOnClickListener(this::onClick);
button4.setOnClickListener(this::onClick);
button5.setOnClickListener(this::onClick);
button6.setOnClickListener(this::onClick);
}
public void onClick (View view)
{
switch (view.getId())
{
case R.id.button1:
try {
textView.setText(textView.getText()+"1");
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button2:
try {
textView.setText(textView.getText()+"2");
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button3:
try {
textView.setText(textView.getText()+"3");
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button4:
try {
textView.setText(textView.getText()+"4");
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button5:
try {
textView.setText(textView.getText()+"5");
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button6:
try {
textView.setText(textView.getText()+"6");
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
输出结果:
简略介绍完VR、AR、MR後,把接下要介绍的是SR。 SR(替代实境):是一种裸眼3D,是一种把虚拟...
防火墙 GCP VPC防火墙规则,应用於给定的项目和网络,防火墙规则可以包含IPv4 IPv6 范围...
前面28天我们已经掌握了如何利用公开资料集来做一些基础的资料视觉化报表,不过 tableau pub...
前言 走过了资料分析、演算法选择後, 我们得知了有些可以改善模型的方向: 解决资料不平衡 学习率的设...
到了第11天,终於回归主题了,今天就来开始串接永丰的api吧! 本来是这麽想的,不过突然发现... ...