就是筆記~
被操作要有ID命名
按紐=toggleButton
常用switch
黃色!可以~ 紅色!不行因為代表錯誤
要確認4邊至少要有一邊有綁
按紐=Button (沒有綁ID)
程式碼新增:
package com.huang.myapp01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
}
}
宣告變數
input=findViewById(R.id.input)
找到(R.id.input)傳到input
初始化元件
package com.huang.myapp01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
EditText input;
ToggleButton toggleButton;
Switch switch1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=findViewById(R.id.input);
toggleButton=findViewById(R.id.toggleButton);
switch1=findViewById(R.id. switch1);
}
public void onClick(View view){
}
}
賦予功能:toggleButton.setOnCheckedChangeListener();
把元件附上設定:檢查是否改變,當被改變要在()裡面填去執行
打new Com可以選
但是舊版的長的不同..要用手改public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
package com.huang.myapp01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
EditText input;
ToggleButton toggleButton;
Switch switch1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=findViewById(R.id.input);
toggleButton=findViewById(R.id.toggleButton);
switch1=findViewById(R.id. switch1);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
}
public void onClick(View view){
}
}
放入if…else
畫面短暫出現的原件Toast
if(isChecked){
Toast.makeText().show();
}
else{}
在這個頁面顯示
f(isChecked){
Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_SHORT).show();
}
else{}
else{}裡面也寫
沒有出現錯誤
package com.huang.myapp01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
EditText input;
ToggleButton toggleButton;
Switch switch1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = findViewById(R.id.input);
toggleButton = findViewById(R.id.toggleButton);
switch1 = findViewById(R.id.switch1);
//toggleButton賦予功能
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
}
}
});
//switch1賦予功能
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
}
}
});
}
public void onClick(View view) {
}
}
但是我按模擬器下面一直顯示E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x000082da
似乎就是要連真的手機
有時是模擬器卡住~要看下面有沒有跑出來~
沒有V有GPU功能的模擬器是方形的
Toast跳太快截不到圖
package com.huang.myapp01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText input;
ToggleButton toggleButton;
Switch switch1;
String str1, str2, str3;
//初始化元件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = findViewById(R.id.input);
toggleButton = findViewById(R.id.toggleButton);
switch1 = findViewById(R.id.switch1);
//toggleButton賦予功能
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
str1 = "toggleButton-->OK";
}
else{
Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
str1 = "toggleButton-->NO";
}
}
});
//switch1賦予功能
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
str2 = "switch1-->OK";
}
else{
Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
str2 = "switch1-->OK";
}
}
});
}
//透過 button 收集全部的資料
public void onClick(View view) {
str3 = input.getText().toString();
Toast.makeText(MainActivity.this,
str1+"\n"+str2+"\n"+str3,
Toast.LENGTH_LONG).show();
}
}
package com.huang.myapp01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText input;
ToggleButton toggleButton;
Switch switch1;
String str1, str2, str3;
//初始化元件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = findViewById(R.id.input);
input.setText("NO INPUT");
str1 = "toggleButton-->NO";
str2 = "switch1-->NO";
str3 = input.getText().toString();
toggleButton = findViewById(R.id.toggleButton);
switch1 = findViewById(R.id.switch1);
//toggleButton賦予功能
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
str1 = "toggleButton-->OK";
}
else{
Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
str1 = "toggleButton-->NO";
}
}
});
//switch1賦予功能
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
str2 = "switch1-->OK";
}
else{
Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
str2 = "switch1-->OK";
}
}
});
}
//透過 button 收集全部的資料
public void onClick(View view) {
str3 = input.getText().toString();
Toast.makeText(MainActivity.this,
str1+"\n"+str2+"\n"+str3,
Toast.LENGTH_LONG).show();
}
}
但是…發現…我的模擬器..原來建的”請輸入姓名”的地方會不見
還是我要把他往下移
可能畫面過長, 可改變對上的距離
NAME的地方還會被疊到
用完要用close關掉才不會佔記憶體
另一個~專案開始-單選RadioButton+複選CheckBox
開工~
Group先做再做button
button一定會縮排-要有2個選項要選2個button
Compontaint Tree預設是由上往下排
要橫的排要放入Layouts
要記得綁定
放checkBox
Layouts不好調-按紐和字都會跑入~要靠移動旁邊的tree
或許調程式碼還比較快
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="調查表"
android:textSize="36sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="409dp"
android:layout_height="139dp"
android:layout_marginTop="36dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CheckBox" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CheckBox" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CheckBox" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
要GROUP去管理...所以要有ID
button也重命名ID
如果text不能改就要從CODE去改
到onClick的orientation可以看到變橫的horizontal
讓linerlayout跟著副容器的大小
在Declared Attributs裡layout_wiudth和match_wiudth 選wrap_content或0dp
然後讓邊邊有留邊要選到padding
命名checkbox的變數
Button-onCilck可以動做
回到程式碼.java檔
package com.huang.myui3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
}
}
預先被V只能選一個
回到程式碼.java檔
從放入變數開始~
package com.huang.myui3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioGroup sex;
CheckBox c1,c2,c3;
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
}
}
顯示字串:String all=””;
就是顯示有人打的東西
package com.huang.myui3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioGroup sex;
CheckBox c1,c2,c3;
TextView show;
String all="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
}
}
繼續寫.java程式碼=讓他去檢查誰被勾選
sex=findViewById(R.id.sex);
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup grroup, int checkedId) {
}
});
新增變數String s1,s2;
逐一檢查:
package com.huang.myui3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup sex;
CheckBox c1,c2,c3;
TextView show;
String all="";
String s1,s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sex = findViewById(R.id.sex);
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.r1:
s1 = "先生";
Toast.makeText(MainActivity.this,
s1,
Toast.LENGTH_SHORT).show();
break;
case R.id.r2:
s1 = "女士";
Toast.makeText(MainActivity.this, s1, Toast.LENGTH_SHORT).show();
break;
case R.id.r3:
s1 = "不顯示";
Toast.makeText(MainActivity.this, s1, Toast.LENGTH_SHORT).show();
break;
}
}
});
//CheckBox
}
public void onClick(View view) {
}
}
再來是各做各的CheckBox
c1=findViewById(R.id.c1);
c2=findViewById(R.id.c2);
c3=findViewById(R.id.c3);
加入偵聽器c1.setOnCheckedChangeListener(new MM());..看自己有沒有被V
都命名MM是制定的
c1.setOnCheckedChangeListener(new MM());
c2.setOnCheckedChangeListener(new MM());
c3.setOnCheckedChangeListener(new MM());
還有最下面的textview
package com.huang.myui3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup sex;
CheckBox c1,c2,c3;
TextView show;
String all="";
String s1,s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sex = findViewById(R.id.sex);
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.r1:
s1 = "先生";
Toast.makeText(MainActivity.this,
s1,
Toast.LENGTH_SHORT).show();
break;
case R.id.r2:
s1 = "女士";
Toast.makeText(MainActivity.this, s1, Toast.LENGTH_SHORT).show();
break;
case R.id.r3:
s1 = "不顯示";
Toast.makeText(MainActivity.this, s1, Toast.LENGTH_SHORT).show();
break;
}
}
});
//CheckBox
c1=findViewById(R.id.c1);
c2=findViewById(R.id.c2);
c3=findViewById(R.id.c3);
c1.setOnCheckedChangeListener(new MM());
c2.setOnCheckedChangeListener(new MM());
c3.setOnCheckedChangeListener(new MM());
show=findViewById(R.id.show);
}
public void onClick(View view) {
}
}
加入checkBOX偵聽器
要選下面的~因為是內部類-要往上搬
反紅的地方往前按燈泡/右鍵general/alt+insert也可以選implement
private class MM implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
}
要使用CheckBox相關的功能指令轉換
CheckBox box = (CheckBox)buttonView;
取出字面上的名稱
String name = box.getText().toString();
是被選到還是被取消
if(isChecked){
Toast.makeText(MainActivity.this, name+"被選中", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, name+"被取消", Toast.LENGTH_SHORT).show();
}
}
}
按鈕蒐集資料顯示在show
public void onClick(View view) {
}
宣告物件被取出來
for(int i:cbs){
//CheckBox cc = findViewById(i);
cc = findViewById(i);
if(cc.isChecked()){
s2 += cc.getText()+"\t";
}
偵聽器可以被改寫
//c1.setOnCheckedChangeListener(new MM());
//c2.setOnCheckedChangeListener(new MM());
//c3.setOnCheckedChangeListener(new MM());
改寫成
c1 = findViewById(R.id.c1);
c2 = findViewById(R.id.c2);
c3 = findViewById(R.id.c3);
package com.huang.myui3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//宣告變數
RadioGroup sex;
CheckBox c1, c2, c3;
TextView show;
String all="";
String s1="先生", s2="沒有點選";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//RadioGroup
sex = findViewById(R.id.sex);
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.r1:
s1 = "先生";
Toast.makeText(MainActivity.this, s1, Toast.LENGTH_SHORT).show();
break;
case R.id.r2:
s1 = "女士";
Toast.makeText(MainActivity.this, s1, Toast.LENGTH_SHORT).show();
break;
case R.id.r3:
s1 = "不顯示";
Toast.makeText(MainActivity.this, s1, Toast.LENGTH_SHORT).show();
break;
}
}
});
//CheckBox
c1 = findViewById(R.id.c1);
c2 = findViewById(R.id.c2);
c3 = findViewById(R.id.c3);
//c1.setOnCheckedChangeListener(new MM());
//c2.setOnCheckedChangeListener(new MM());
//c3.setOnCheckedChangeListener(new MM());
show = findViewById(R.id.show);
}
//CheckBox的偵聽器:內部類
private class MM implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//判斷元件
CheckBox box = (CheckBox)buttonView;
//取出元件的名稱
String name = box.getText().toString();
//判斷點選狀態
if(isChecked){
Toast.makeText(MainActivity.this, name+"被選中", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, name+"被取消", Toast.LENGTH_SHORT).show();
}
}
}
//按鈕收集資料,顯示在show
public void onClick(View view) {
//先清空s2
s2 = "";
//收集CheckBox的結果
int[] cbs = {R.id.c1, R.id.c2, R.id.c3};
CheckBox cc;
for(int i:cbs){
//CheckBox cc = findViewById(i);
cc = findViewById(i);
if(cc.isChecked()){
s2 += cc.getText()+"\t";
}
}
//判斷文字欄位是否為空
//TextUtils需 import android.text.TextUtils;(舊版)
if(TextUtils.isEmpty(s2)){
s2 = "沒有點選";
}
all = s1+"\n"+s2;
show.setText(all);
}
}