androidswitch控制項
A. android使用switchcase頁面跳
android使用switchcase頁面跳
qq_45973003
關注
Android中Activity的開啟Activity頁面的跳轉詳解 原創
2020-10-24 14:20:09
1點贊
qq_45973003
碼齡3年
關注
android開啟和關閉activity:
1:在android:中我們要開啟和關閉activity按鈕首先就要創建兩個activity
2:然後在他們的布局文件中添加頁面
3:然後使用java代碼編寫程序實現頁面的開啟和關閉
在這里插入圖片描述
在MainActyvity中的java代碼:
package com.example.open_and_close_activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第一步想要編寫activity的響應式就要先初始化這兩個控制項,設置的是點擊事件的監聽器實現的方式是通過介面的方式來進行實現
findViewById(R.id.start).setOnClickListener(this);
findViewById(R.id.finish).setOnClickListener(this);
}
//這就是我們用到的那個介面
@Override
public void onClick(View view) {
//在onactivity中編寫點擊按鈕的代碼,它的裡面有兩個參數,第一個view代表的是被點擊的view
// 1:使用switch方法來獲取被點擊按鈕的id
switch(view.getId()){
//使用case語句將兩個按鈕的id列舉出來
case R.id.start: //開啟activity的按鈕
//編寫實現它的代碼邏輯創建一個Intent對象,第一個參數表示的是上下文的信息,第二個參數表示的是我們要開啟的activity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
break;
case R.id.finish: //關閉activity的按鈕
//關閉activity使用的是Button2方法
finish();
break;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
B. android studio switch控制項怎麼使用
adroid studio switch控制項的用法如下:
Switch sc=(Switch)findViewById(R.id.switch1);
sc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
Toast.makeText(getApplicationContext(),"on",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show();
}
}
});
C. android studio switch控制項怎麼使用
didn't think that I had hurt
D. android switch控制項的注冊監聽控制
你一定看錯了,一個OnClickListener只會對一個view有效,不存在和父控制項沖突問題。如果有問題你可以設置setOnClickListener(new
OnClickListener(){
public
void
click(View
v){
log.d("------",
v.getId());
}
});
給view設置上id,看看是誰
E. androidswitch設置最小長度無效
不在范圍。androidswitch是Android自帶的開關控制項,而在Android工具中設置switch最小長度無效,那是因為設置的數字並不在范圍內,因此就導致是無效的。
F. android中switch控制項怎麼控制圖片的顯示隱藏
Android中自帶的Switch控制項在很多時候總覺得和整體系統風格不符,很多時候,自定義Switch是一種方法。
但其實不用這么麻煩,安卓自帶的Switch通過修改一些屬性,也可以達到和自定義Switch差不多的一個效果。
個人感覺,Switch的屬性設置和其他控制項還是有挺大區別的。
實現方式:
底部滑動條,在開關打開狀態為綠色,開關關閉狀態為灰色
在 res/drawable 文件夾下面,寫兩個滑動條的底圖 ,通過一個選擇器selector進行控制。
G. Android自定義Switch
由於最近需要實現一個自定義的switch,本想寫一個MySwitch繼承Switch來進行自定義,但是後來發現其實只需要通過定義switch的thumb和track的圖片來達到自定義switch樣式的目的。
在res/drawable內先定義一個軌道關閉狀態的drawable,switch_gray_track.xml
再定義一個軌道開啟狀態的drawable,switch_yellow_track.xml
最後定義一個selector作為軌道的樣式,switch_track.xml
在res/drawable內定義一個滑動塊的drawable,switch_thumb.xml
增加了一個透明的描邊,不然白色的小滑塊就完全覆蓋掉了外面的軌道。
修改android:layout_width,android:layout_height 這兩個屬性,並不會實際修改Switch的大小設置大了,邊上會出現空白部分,設置小了,Switch顯示不全。
Switch的高度是由track的高度決定的,寬度呢則可以通過設置switchMinWidth(開關最小寬度 )來更改。
github: https://github.com/JeremySun823/MySwitchTest
H. android中switch控制項怎麼判斷開關
1234567891011public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (view.getId()) { case R.id.switch1: if(isChecked) { statusText.setText("開"); } else { statusText.setText("關"); } break; .....}不知道這是不是你要的效果
I. Android中switch控制項的使用
點擊查看
J. AndroidUI控制項switch使用方法
首先添加控制項:
<Switch
android:id="@+id/sw_sfktmmzf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:showText="false"
android:switchMinWidth="50dp"
android:thumb="@drawable/thumb"
android:track="@drawable/track" />
以下是該控制項的常用屬性:
textOn:控制項打開時顯示的文字
textOff:控制項關閉時顯示的文字
thumb:控制項開關的圖片(設置小圓圈顏色)
track:控制項開關的軌跡圖片(設置小圓圈背景顏色)
typeface:設置字體類型
switchMinWidth:開關最小寬度
switchPadding:設置開關 與文字的空白距離
switchTextAppearance:設置文本的風格
checked:設置初始選中狀態
splitTrack:是否設置一個間隙,讓滑塊與底部圖片分隔(API 21及以上)
showText:設置是否顯示開關上的文字(API 21及以上)
創建北京控制文件在drawable文件下
1、thumb.xml
<?xml version="1.0" encoding="utf-8"?><!-- 按鈕的選擇器,可以設置按鈕在不同狀態下的時候,按鈕不同的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/green_thumb" android:state_checked="true" />
<item android:drawable="@drawable/gray_thumb" />
顏色文件:
green_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="@dimen/switch_height" android:width="@dimen/switch_height"/>
<!-- 圓角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 變化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="@color/home_text1"/>
</shape>
gray_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="@dimen/switch_height" android:width="@dimen/switch_height"/>
<!-- 圓角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 變化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="@color/text_color03"/>
</shape>
2、track.xml
<?xml version="1.0" encoding="utf-8"?><!-- 底層下滑條的樣式選擇器,可控制Switch在不同狀態下,底下下滑條的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/green_track" android:state_checked="true" />
<item android:drawable="@drawable/gray_track" />
</selector>
顏色文件:
green_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 高度40 -->
<size android:height="@dimen/switch_height"/>
<!-- 圓角弧度 20 -->
<corners android:radius="15dp"/>
<!-- 變化率 -->
<gradient
android:endColor="@color/home_text1"
android:startColor="@color/home_text1" />
</shape>
gray_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 高度30 此處設置寬度無效-->
<size android:height="@dimen/switch_height" />
<!-- 圓角弧度 15 -->
<corners android:radius="15dp" />
<!-- 變化率 定義從左到右的顏色不變 -->
<gradient
android:endColor="@color/text_color03"
android:startColor="@color/text_color03" />
</shape>
switch 控制項監聽事件:
aSwitch.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//控制開關字體顏色
if(isChecked) {
//打開
}else{
//關閉
}
}
});