Android拖动条的实现代码
程序员文章站
2023-12-18 14:25:58
本文实例为大家分享了android拖动条的具体代码,供大家参考,具体内容如下
activity_main.xml
本文实例为大家分享了android拖动条的具体代码,供大家参考,具体内容如下
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="com.amy.seekbartest.mainactivity"> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前值:50" android:textsize="16sp" android:paddingtop="40dp" android:paddingleft="15dp" tools:layout_editor_absolutey="0dp" tools:layout_editor_absolutex="0dp" /> <!-- 拖动条 --> <seekbar android:id="@+id/seekbar1" android:layout_height="wrap_content" android:max="100" android:layout_width="match_parent" android:progress="50" android:paddingtop="10dp" tools:layout_editor_absolutey="0dp" tools:layout_editor_absolutex="32dp" /> </android.support.constraint.constraintlayout>
mainactivity.java
package com.amy.seekbartest; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.seekbar; import android.widget.textview; import android.widget.toast; public class mainactivity extends appcompatactivity { private seekbar seekbar;//拖动条 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final textview result = (textview) findviewbyid(r.id.textview1); seekbar = (seekbar) findviewbyid(r.id.seekbar1); seekbar.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { result.settext("当前值:"+progress); } @override public void onstarttrackingtouch(seekbar seekbar) { toast.maketext(mainactivity.this,"开始滑动",toast.length_short).show(); } @override public void onstoptrackingtouch(seekbar seekbar) { toast.maketext(mainactivity.this,"结束滑动",toast.length_short).show(); } }); } }
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。