当前位置:首页 » 安卓系统 » android开关按钮

android开关按钮

发布时间: 2022-02-25 14:37:54

‘壹’ android中怎样获取togglebutton的开关值

可以用:toggleButton.isChecked();来拿到ToggleButton的开关值。

‘贰’ android 按钮控制其他Activity中的控件显示

将about中的开关状态保存起来,main界面每次显示时读取这个状态来控制textview的显示和隐藏

‘叁’ android滑动开关按钮实现方式有几种

关于Android中View控件的分类可以分为以下几类:
1. 文本类:
TextView、EditText、AutoCompleteTextView、MultAutoCompletTextView 、(TextSwitcher) 、(DigitalClock)
ExtractEditText、CheckedTextView、Chronometer

2.按钮类:
Button、CheckBox、RadioButton(RadioGroup) 、ToggleButton 、(ImageButton ) CompoundButton

2. 缩放按钮:
ZoomButton、ZoomControls

3. 图片类:
ImageView、ZoomButton、ImageButton、(ImageSwitcher ) QuickContactBadge

4. 时间控件:
DigitalClock、AnalogClock、TimePicker、DatePicker

5.进度显示:
ProgressBar、AbsSeekBar、SeekBar、RatingBar(星星评分)

6.导航: TabHost、TabWidget。

7.视频媒体:
VideView、MediaController

8.Dialog对话框
CharacherPickerDialog、AlertDialog、DatePickerDialog、ProgressDialog、TimePickerDialog

9. 布局类控件:
AbsoluteLayout、LinearLayout、RadioGroup 、TableLayout、 TableRow、RelativeLayout、FrameLayout

10.需要适配器的布局类:
AdapterView、AbsListView、GridView、ListView、AbsSpinner、Gallery Spinner

11.滚动条: HorizontalScrollView、ScrollView

12.网页: WebView

13.动画: ViewAimator、ViewFilpper、ViewSwitcher、ImageSwitcher、TextSwitcher

‘肆’ android自定的开关按钮不用点击怎么改变开关状态

在Android中,不管什么样子的视图都是继承自View类,因此我们必须要自定义一个View类,下面看一下代码实现

‘伍’ 安卓手机如何把开机键改成其他的按键

修改系统文件的代码,可以让其他按键代替电源键的锁屏功能。

可代码毕竟是软件,只有在系统运行时才起作用,想完全代替电源键是不可能的。电源键的开、关机功能除了是系统文件决定,最基本的还是硬件电路决定的,所以不建议把开机键改成其它的按键。

‘陆’ android开关按钮怎么实现

可以直接用button和两张图片(开和关),用图片做背景,在点击事件中切换图片就行了

‘柒’ 安卓怎么把两个按钮写成一个,点一下开,再点一下关

  • 定义一个全局变量,比如定义一个boolean类型的,首先初始值设置成false,,点击开关,首先判断该boolean是true还是false,如果是false,就打开灯,并且将该boolean赋值为true,在点击开关时,会再判断该boolean是true还是false,这时候boolean是true,就执行关灯,然后在把boolean设为false,代码如下:

  • java">Booleanboolean=false;
    findViewById(R.id.open_btn).setOnClickListener(newOnClickListener(){
    @Override
    publicvoidonClick(Viewarg0){
    if(boolean==false){
    if("楼道灯".equals(mControlName)){
    sendCMD(streetOpenCommand);
    }elseif("路灯".equals(mControlName)){
    sendCMD(corridorOpenCommand);
    }
    boolean=true;
    }elseif(boolean==true){
    if("楼道灯".equals(mControlName)){
    sendCMD(streetCloseCommand);
    }elseif("路灯".equals(mControlName)){
    sendCMD(corridorCloseCommand
    );
    }
    boolean=false;
    }
    }
    });

‘捌’ 在Android中作一个界面按钮,类似于WIFI的开关

Switch控件

‘玖’ 怎么在android studio中实现开关按钮

先在布局文件中添加一个Button,然后再在java代码中获取这个按钮,设置它的监听事件就可以了。
.xml:
<Button android:"@+id/button1"......>
.java:
Button btn = (Button) findViewById(R.id.button1);
btn .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//你要执行的代码
}
});
这是一种方法

‘拾’ android中的switch开关和ios中的不同,这样就需要通过动画来实现一个iphone开关了

做IOS开发的都知道,IOS提供了一个具有动态开关效果的UISwitch组件,这个组件很好用效果相对来说也很绚丽,当我们去点击开关的时候有动画效果,但遗憾的是Android上并没有给我们提供类似的组件(听说在Android4.0的版本上提供了具有动态效果的开关组件,不过我还没有去看文档),如果我们想实现类似的效果那该怎么办了呢?看来又得去自定义了。
公司的产品最近一直在做升级,主要做的就是把界面做的更绚丽更美观给用户更好的体验(唉,顾客是上帝......),其中的设置功能中就有开关按钮,原来的开关做的是两幅图片,通过点击图片来给开关设置不同的状态图片,但是这种效果很死板和程序的整体风格不太协调,于是就想着实现类似于IOS中的开关效果。
拿着笔在图纸上画了画,我实现的原理也是采用了两幅图片,一个整体的背景图:和一个小滑块图:,用小滑块图实现在背景图上滑动,当小滑块滑到左边时恰好遮挡了开字,就是显示的关了,同样原理当小滑块滑动到右侧时恰好遮挡了关字也就是现实开了,滑动的实现主要用的就是TranslateAnimation类,如有对TranslateAnimation不太熟悉的,问问度娘,她那有关TranslateAnimation的解说多了去了,毕竟自己动手,丰衣食足嘛,(*^__^*) 嘻嘻……
好了,老规矩来看一下项目结构吧:

工程中switch_button.xml文件就是对应的SwitchButton的布局文件,内容不需要解释,你一看就懂

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switch_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/switch_bg">

<ImageView
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/switch_btn" />

</LinearLayout>

SwitchButton的布局文件中根节点是个LinearLayout,把它的背景设置成了一个含有开关文字的图片,里边包含一个ImageView,这个ImageView就是用来在LinearLayout中进行滑动的。
其中自定义开关组件就是都在wedgit包下的SwitchButton,那么赶紧来看一下SwitchButton的代码吧

?

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

public class SwitchButton extends LinearLayout {

/**
* 开关图片
*/
private LinearLayout switchParent;
/**
* 滑块图片
*/
private ImageView switchButton;
/**
* 按钮状态,默认关闭
*/
private boolean isOn = false;
/**
* 滑块需要滑动的距离
*/
private int scrollDistance;
/**
* 开关按钮监听器
*/
private SwitchChangedListner listner;

public SwitchButton(Context context) {
super(context);
initWedgits(context);
}

public SwitchButton(Context context, AttributeSet attrs) {
super(context, attrs);
initWedgits(context);
}

/**
* 初始化组件
*
* @param context
* 上下文环境
*/
private void initWedgits(Context context) {
try {
View view = LayoutInflater.from(context).inflate(
R.layout.switch_button, this);
switchParent = (LinearLayout) view.findViewById(R.id.switch_parent);
switchButton = (ImageView) view.findViewById(R.id.switch_button);
addListeners();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 添加事件监听器
*/
private void addListeners() {
try {
switchParent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
isOn = !isOn;
scrollSwitch();
if (null != listner) {
// 开关开发或者关闭的回调方法
listner.switchChanged(getId(), isOn);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 滑动开关
*/
private void scrollSwitch() {
// 获取滑块需要滑动的距离,滑动距离等于父组建的宽度减去滑块的宽度
scrollDistance = switchParent.getWidth() - switchButton.getWidth();
// 初始化滑动事件
Animation animation = null;
if (isOn) {
animation = new TranslateAnimation(0, scrollDistance, 0, 0);
} else {
animation = new TranslateAnimation(scrollDistance, 0, 0, 0);
}
// 设置滑动时间
animation.setDuration(200);
// 滑动之后保持状态
animation.setFillAfter(true);
// 开始滑动
switchButton.startAnimation(animation);
}

/**
* 获取开关状态
*
* @return 【true:打开】【false:关闭】
*/
public boolean isOn() {
return isOn;
}

/**
* 设置开关状态
*
* @param isOn
* 开关状态【true:打开】【false:关闭】
*/
public void setOn(boolean isOn) {
if (this.isOn == isOn) {
return;
}
this.isOn = isOn;
post(new Runnable() {
@Override
public void run() {
scrollSwitch();
}
});
}

/**
* 设置开关状态监听器
*
* @param listner
* 开关状态监听器
*/
public void setOnSwitchListner(SwitchChangedListner listner) {
this.listner = listner;
}

/**
* 开关状态监听器
*
* @author llew
*
*/
public interface SwitchChangedListner {
/**
* 开关状态改变
*
* @param viewId
* 当前开关ID
* @param isOn
* 开关是否打开【true:打开】【false:关闭】
*/
public void switchChanged(Integer viewId, boolean isOn);
}
}

热点内容
收件服务器和发件服务器如何设定 发布:2024-05-08 21:47:55 浏览:834
搭建节点ip用什么服务器 发布:2024-05-08 21:46:58 浏览:126
派什么编程 发布:2024-05-08 21:25:43 浏览:340
手机作为存储设备 发布:2024-05-08 21:23:34 浏览:493
苹果x配置如何看 发布:2024-05-08 21:15:34 浏览:32
易语言加密文件夹 发布:2024-05-08 21:15:33 浏览:695
b站如何知道账号密码 发布:2024-05-08 20:33:55 浏览:691
知识图谱算法 发布:2024-05-08 20:33:19 浏览:908
手机登云服务器windows 发布:2024-05-08 20:32:57 浏览:113
上传3d模型 发布:2024-05-08 20:11:41 浏览:604