前言
自己整理的总结,方便使用时直接复制
一、拖动条
布局文件
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:max="100" //设置滑动最大值
android:progress="80" /> //设置默认值
java调用
SeekBar seekbar= (SeekBar) findViewById(R.id.SeekBar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//进度发生改变时会触发
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//progress参数为改变后的进度值
}
//按住SeekBar时会触发
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
//放开SeekBar时触发
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
二、开关
1.开关按钮
布局文件
android:disabledAlpha:设置按钮在禁用时的透明度
android:textOff:按钮没有被选中时显示的文字
android:textOn:按钮被选中时显示的文字
<ToggleButton
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textOff="已打开"
android:textOn="已关闭" />
java调用
ToggleButton btn = (ToggleButton) findViewById(R.id.btn);
btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Toast.makeText(MainActivity.this, "打开", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_SHORT).show();
}
}
});
2.开关图标
布局文件
android:showText:设置on/off的时候是否显示文字,boolean
android:splitTrack:是否设置一个间隙,让滑块与底部图片分隔,boolean
android:switchMinWidth:设置开关的最小宽度
android:switchPadding:设置滑块内文字的间隔
android:switchTextAppearance:设置开关的文字外观,暂时没发现有什么用…
android:textOff:按钮没有被选中时显示的文字
android:textOn:按钮被选中时显示的文字
android:textStyle:文字风格,粗体,斜体写划线那些
android:track:底部的图片
android:thumb:滑块的图片
android:typeface:设置字体,默认支持这三种:sans, serif, monospace
<Switch
android:id="@+id/swh_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:showText="true"
android:textOff="1"
android:textOn="2" />
java调用
Switch swh_status = (Switch) findViewById(R.id.swh_status);
swh_status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Toast.makeText(MainActivity.this, "打开", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_SHORT).show();
}
}
});
三、单选框
布局文件
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btn_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正确" />
<RadioButton
android:id="@+id/btn_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="错误" />
</RadioGroup>
java调用
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radbtn = (RadioButton) findViewById(checkedId);
Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show();
}
});
四、复选框
布局文件
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<CheckBox
android:id="@+id/chb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="苹果"/>
<CheckBox
android:id="@+id/chb2"
android:layout_toRightOf="@+id/chb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雪梨"/>
<CheckBox
android:id="@+id/chb3"
android:layout_toRightOf="@+id/chb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可乐"/>
<Button
android:id="@+id/btn_checkbox"
android:layout_toRightOf="@+id/chb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" />
</RelativeLayout>
java调用
Button btn_checkbox = (Button)findViewById(R.id.btn_checkbox);
btn_checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb_one = (CheckBox) findViewById(R.id.chb1);
CheckBox cb_two = (CheckBox) findViewById(R.id.chb2);
CheckBox cb_three = (CheckBox) findViewById(R.id.chb3);
String choose = "";
if(cb_one.isChecked()){
choose += cb_one.getText().toString() + "";
}
if(cb_two.isChecked()){
choose += cb_two.getText().toString() + "";
}
if(cb_three.isChecked()){
choose += cb_three.getText().toString() + "";
}
Toast.makeText(MainActivity.this,choose,Toast.LENGTH_SHORT).show();
}
});
五、星级评分条
android:isIndicator:是否用作指示,用户无法更改,默认false
android:numStars:显示多少个星星,必须为整数
android:rating:默认评分值,必须为浮点数
android:stepSize: 评分每次增加的值,必须为浮点数
布局文件:
<RatingBar
android:id="@+id/ratingBar"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="7"
android:rating="3.5"
android:stepSize="0.5"/>
java调用
RatingBar ratingBar = (RatingBar)findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(MainActivity.this, "rating:" + String.valueOf(rating), Toast.LENGTH_LONG).show();
}
});
六、滚动条
布局文件
<ScrollView
android:id="@+id/scView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
java调用
ScrollView scView = (ScrollView)findViewById(R.id.scView);
scView.fullScroll(ScrollView.FOCUS_DOWN); //滚动到底部
scView.fullScroll(ScrollView.FOCUS_UP); //滚动到顶部
七、弹框提醒
1.简单用法
(本页面java文件名.this,“弹框内容”,显示时间(LONG和SHORT))
Toast.makeText(MainActivity.this,"已清空输入!", Toast.LENGTH_SHORT).show();
2.设置属性
Toast toast = Toast.makeText(MainActivity.this,"已清空输入!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL , 0, 0); //设置显示位置
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.YELLOW); //设置字体颜色
toast.show();
3.通过构造方法定制Toast
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.layout, (ViewGroup) findViewById(R.id.lly_toast));
ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo);
TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
tv_msg.setText("已清空输入!");
Toast toast = new Toast(MainActivity.this);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();