zoukankan      html  css  js  c++  java
  • seekbar拖动条控件

    seekbar拖动条控件可以通过拖动条改变当前的值,可以用seebar设置具有一定范围的变量值。如:

    在.xml文件中:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:id="@+id/textview1" android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <TextView android:id="@+id/textview2" android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <SeekBar android:id="@+id/seekbar1" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:max="100"
            android:progress="30"></SeekBar>
    
        <SeekBar android:id="@+id/seekbar2" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:max="100"
            android:progress="30" android:secondaryProgress="60"></SeekBar>
    </LinearLayout>

    在.java文件中:

    public class Main extends Activity implements OnSeekBarChangeListener {
        /** Called when the activity is first created. */
        private TextView textView1, textView2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            textView1 = (TextView) this.findViewById(R.id.textview1);
            textView2 = (TextView) this.findViewById(R.id.textview2);
            SeekBar seekBar1 = (SeekBar) this.findViewById(R.id.seekbar1);
            SeekBar seekBar2 = (SeekBar) this.findViewById(R.id.seekbar2);
            seekBar1.setOnSeekBarChangeListener(this);
            seekBar2.setOnSeekBarChangeListener(this);
        }
    
        // 当滑动滑竿的时候会触发的事件
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            if (seekBar.getId() == R.id.seekbar1) {
                textView1.setText("seekbar1的当前位置是:" + progress);
            } else {
                textView2.setText("seekbar2的当前位置是:" + progress);
            }
        }
    
        // 表示从哪里开始拖动
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            if (seekBar.getId() == R.id.seekbar1) {
                textView1.setText("seekbar1开始拖动");
            } else {
                textView1.setText("seekbar2开始拖动");
            }
        }
    
        // 表示从哪里结束拖动
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            if (seekBar.getId() == R.id.seekbar1) {
                textView1.setText("seekbar1停止拖动");
            } else {
                textView1.setText("seekbar2停止拖动");
            }
        }
    }

    运行结果:

  • 相关阅读:
    条款十四 在资源管理类中小心copying行为
    条款八 别让异常逃离析构函数
    条款五 了解C++默默的编写并调用的哪些函数
    volatile——C++关键字(转)
    C++ auto_ptr(转)
    条款十三 以对象管理资源
    优秀文章收集(更新中..)
    条款十一 在operator = 中处理"自我赋值"
    TCP协议疑难杂症全景解析(转)
    大四的迷茫
  • 原文地址:https://www.cnblogs.com/SoulCode/p/5370091.html
Copyright © 2011-2022 走看看