Ⅰ android中switch控制項怎麼控制圖片的顯示隱藏
Android中自帶的Switch控制項在很多時候總覺得和整體系統風格不符,很多時候,自定義Switch是一種方法。
但其實不用這么麻煩,安卓自帶的Switch通過修改一些屬性,也可以達到和自定義Switch差不多的一個效果。
個人感覺,Switch的屬性設置和其他控制項還是有挺大區別的。
實現方式:
底部滑動條,在開關打開狀態為綠色,開關關閉狀態為灰色
在 res/drawable 文件夾下面,寫兩個滑動條的底圖 ,通過一個選擇器selector進行控制。
Ⅱ android SwitchButton 和CheckBox 怎樣設置不可點擊,Clickable無效
在線程還在進行時設置按鈕不可點擊setClickable(false);
getState()返回該線程的狀態;
isAlive()測試線程是否處於活動狀態;
view都是setClickable(false); 表示不可點擊的;
當然你還可以直接注冊一個Touche事件,然後在onTouche里return true;
Ⅲ 安卓switch按鈕圖標縮小怎麼設置
按鈕圖標,可以使用ImageView 或者ImageButton. 對應三張圖片的話,就用三張不同的圖片代表就成了。 然後實現它們各自的OnClickListener。
Ⅳ android switch控制項的注冊監聽控制
你一定看錯了,一個OnClickListener只會對一個view有效,不存在和父控制項沖突問題。如果有問題你可以設置setOnClickListener(new
OnClickListener(){
public
void
click(View
v){
log.d("------",
v.getId());
}
});
給view設置上id,看看是誰
Ⅳ android開發中switch在切換開/關狀態時,先彈出一個dialog(確認/取消)以讓用戶確認是否切換開/關狀態
要實現這種邏輯,方式實在太多了,最簡單的就是switch注冊點擊事件
(不叫你注冊onCheckedChanged事件是因為你設置setChecked的時候,又會觸發onCheckedChanged)
然後在方法里先彈出一個alertDialog
確定後設置switch.setChecked(true/false)
Ⅵ android 怎樣在switch裡面設置簡訊倒計時
1我們首先需要在輸入框中輸入一個秒數,比如:12,然後獲取輸入框中的值,顯示在一個TextView中;
2點擊「開始倒計時」按鈕,每隔一秒鍾,秒數減1,然後顯示在TextView中;
3點擊「停止倒計時」按鈕,計時器停止運行,當再次點擊「開始倒計時」按鈕,計時器繼續運行。
Ⅶ android 自定義switch樣式
修改後的MySwitch控制項介面基本與原Switch控制項一致,並且除了可支持所有SDK外,增加了2項小功能:
1. 支持用Track背景圖片的方式代替Texton Textoff等文字方式表現開關狀態
2.支持調整控制Switch的高度
下面貼出Switch修改的關鍵代碼:
/**
* <p>
* modified from android SDK 14(4.0) android.widget.Switch.
* <br/>
* <strong>new feature: </strong>
* <ol>
* <li>support SDK 1 or higher. </li>
* <li>you can use track drawable instead of text to display the changes of off-on state!</li>
* <li>you can control the Switch minimum height. </li>
* </ol>
* </p>
*
* @see {@link Switch}
* @author Wison
*/
public class MySwitch extends CompoundButton {
// Support track drawable instead of text
private Drawable mTrackOnDrawable;
private Drawable mTrackOffDrawable;
// Support minimum height
private int mSwitchMinHeight;
/**
* Construct a new Switch with a default style determined by the given theme attribute,
* overriding specific style attributes as requested.
*
* @param context The Context that will determine this widget's theming.
* @param attrs Specification of attributes that should deviate from the default styling.
* @param defStyle An attribute ID within the active theme containing a reference to the
* default style for this widget. e.g. android.R.attr.switchStyle.
*/
public MySwitch(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
Resources res = getResources();
mTextPaint.density = res.getDisplayMetrics().density;
//float scaledDensity = res.getDisplayMetrics().scaledDensity;
//mTextPaint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Switch, defStyle, 0);
// off-on 模式: 圖片模式或文字模式,圖片模式是用Track背景圖片表示off-on的狀態,文字模式是用文字來表示off-on狀態。
mTrackOnDrawable = a.getDrawable(R.styleable.Switch_trackOn);
mTrackOffDrawable = a.getDrawable(R.styleable.Switch_trackOff);
if (checkTrackOffOnDrawable()) {
// 如果設定圖片模式,則默認顯示off狀態
mTrackDrawable = mTrackOffDrawable;
} else {
mTrackDrawable = a.getDrawable(R.styleable.Switch_track);
}
mThumbDrawable = a.getDrawable(R.styleable.Switch_thumb);
mTextOn = a.getText(R.styleable.Switch_textOn);
mTextOff = a.getText(R.styleable.Switch_textOff);
mThumbTextPadding = a.getDimensionPixelSize(R.styleable.Switch_thumbTextPadding, 0);
mSwitchMinWidth = a.getDimensionPixelSize(R.styleable.Switch_switchMinWidth, 0);
mSwitchMinHeight = a.getDimensionPixelSize(R.styleable.Switch_switchMinHeight, 0);
mSwitchPadding = a.getDimensionPixelSize(R.styleable.Switch_switchPadding, 0);
int appearance = a.getResourceId(R.styleable.Switch_switchTextAppearance, 0);
if (appearance != 0) {
setSwitchTextAppearance(context, appearance);
}
a.recycle();
ViewConfiguration config = ViewConfiguration.get(context);
mTouchSlop = config.getScaledTouchSlop();
mMinFlingVelocity = config.getScaledMinimumFlingVelocity();
// Refresh display with current params
refreshDrawableState();
setChecked(isChecked());
}
private boolean checkTrackOffOnDrawable() {
return mTrackOnDrawable != null && mTrackOffDrawable != null;
}
@SuppressLint("NewApi")
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mOnLayout == null) {
mOnLayout = makeLayout(mTextOn);
}
if (mOffLayout == null) {
mOffLayout = makeLayout(mTextOff);
}
mTrackDrawable.getPadding(mTempRect);
final int maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth());
final int switchWidth = Math.max(mSwitchMinWidth,
maxTextWidth * 2 + mThumbTextPadding * 4 + mTempRect.left + mTempRect.right);
// final int switchHeight = mTrackDrawable.getIntrinsicHeight();
int switchHeight;
if (mSwitchMinHeight <= 0) {
switchHeight = mTrackDrawable.getIntrinsicHeight();
} else {
switchHeight = Math.max(mSwitchMinHeight, mTempRect.top + mTempRect.bottom);
}
mThumbWidth = maxTextWidth + mThumbTextPadding * 2;
mSwitchWidth = switchWidth;
mSwitchHeight = switchHeight;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int measuredHeight = getMeasuredHeight();
if (measuredHeight < switchHeight) {
if (Build.VERSION.SDK_INT >= 11) {
setMeasuredDimension(getMeasuredWidthAndState(), switchHeight);
} else {
setMeasuredDimension(getMeasuredWidth(), switchHeight);
}
}
}
@Override
public void setChecked(boolean checked) {
if (checkTrackOffOnDrawable()) {
mTrackDrawable = checked ? mTrackOnDrawable : mTrackOffDrawable;
refreshDrawableState();
}
super.setChecked(checked);
mThumbPosition = checked ? getThumbScrollRange() : 0;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw the switch
int switchLeft = mSwitchLeft;
int switchTop = mSwitchTop;
int switchRight = mSwitchRight;
int switchBottom = mSwitchBottom;
if (checkTrackOffOnDrawable()) {
mTrackDrawable = getTargetCheckedState() ? mTrackOnDrawable : mTrackOffDrawable;
refreshDrawableState();
}
mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom);
mTrackDrawable.draw(canvas);
canvas.save()
mTrackDrawable.getPadding(mTempRect);
int switchInnerLeft = switchLeft + mTempRect.left;
int switchInnerTop = switchTop + mTempRect.top;
int switchInnerRight = switchRight - mTempRect.right;
int switchInnerBottom = switchBottom - mTempRect.bottom;
canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom);
mThumbDrawable.getPadding(mTempRect);
final int thumbPos = (int) (mThumbPosition + 0.5f);
int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos;
int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + mTempRect.right;
mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);
mThumbDrawable.draw(canvas);
// mTextColors should not be null, but just in case
if (mTextColors != null) {
mTextPaint.setColor(mTextColors.getColorForState(getDrawableState(),
mTextColors.getDefaultColor()));
}
mTextPaint.drawableState = getDrawableState();
Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
if (switchText != null) {
canvas.translate((thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2,
(switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2);
switchText.draw(canvas);
}
canvas.restore();
}
}
下面是關鍵屬性聲明:
<declare-styleable name="Switch">
<!-- Drawable to use when the switch is in the checked/"on" state. -->
<attr name="trackOn" format="reference" />
<!-- Drawable to use when the switch is in the unchecked/"off" state. -->
<attr name="trackOff" format="reference" />
<!-- Minimum height for the switch component -->
<attr name="switchMinHeight" format="dimension" />
<!-- Drawable to use as the "thumb" that switches back and forth. -->
<attr name="thumb" format="reference" />
<!-- Drawable to use as the "track" that the switch thumb slides within. -->
<attr name="track" format="reference" />
<!-- Text to use when the switch is in the checked/"on" state. -->
<attr name="textOn" format="string" />
<!-- Text to use when the switch is in the unchecked/"off" state. -->
<attr name="textOff" format="string" />
<!-- Amount of padding on either side of text within the switch thumb. -->
<attr name="thumbTextPadding" format="dimension" />
<!-- TextAppearance style for text displayed on the switch thumb. -->
<attr name="switchTextAppearance" format="reference" />
<!-- Minimum width for the switch component -->
<attr name="switchMinWidth" format="dimension" />
<!-- Minimum space between the switch and caption text -->
<attr name="switchPadding" format="dimension" />
</declare-styleable>
Ⅷ 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();
}
}
});
Ⅸ 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{
//關閉
}
}
});