码迷,mamicode.com
首页 > 其他好文 > 详细

SeekBar

时间:2015-09-23 21:09:30      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

SeekBar 界面:

技术分享

 

===================================================================================

代码实现:MainActivity.ja  1 package com.example.aa09_seekbar;    3 import android.os.Bundle;

  4 import android.app.Activity;
  5 import android.content.BroadcastReceiver;
  6 import android.content.Context;
  7 import android.content.Intent;
  8 import android.content.IntentFilter;
  9 import android.util.Log;
 10 import android.view.Menu;
 11 import android.view.View;
 12 import android.view.View.OnClickListener;
 13 import android.widget.Button;
 14 import android.widget.SeekBar;
 15 import android.widget.SeekBar.OnSeekBarChangeListener;
 16 import android.widget.TextView;
 17 
 18 public class MainActivity extends Activity {
 19  private TextView tv_current, tv_max;
 20  private SeekBar seekBar1;
 21  private Button start, stop;
 22  private int myprogress = 0;
 23  private BroadcastReceiver receiver = new BroadcastReceiver() {
 24 
 25   @Override
 26   public void onReceive(Context context, Intent intent) {
 27    int count = intent.getIntExtra("count", 0);
 28    tv_current.setText("当前值:" + count);//文本的改变settext
 29    seekBar1.setProgress(count);//拖动条的progress的改变
 30 
 31   }
 32  };
 33 
 34  @Override
 35  protected void onCreate(Bundle savedInstanceState) {
 36   super.onCreate(savedInstanceState);
 37   setContentView(R.layout.activity_main);
 38   findbyid();
 39   Init();
 40   Listener();
 41  }
 42 
 43  private void Init() {
 44   tv_current.setText("当前值:0");
 45   tv_max.setText("最大值:100");
 46   seekBar1.setProgress(0);
 47   seekBar1.setMax(100);
 48   IntentFilter if1 = new IntentFilter();
 49   if1.addAction("action");
 50   registerReceiver(receiver, if1);
 51 
 52  }
 53 
 54  private void findbyid() {
 55   tv_current = (TextView) findViewById(R.id.tv_current);
 56   tv_max = (TextView) findViewById(R.id.tv_max);
 57   seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
 58   start = (Button) findViewById(R.id.start);
 59   stop = (Button) findViewById(R.id.stop);
 60 
 61  }
 62 
 63  private void Listener() {
 64   start.setOnClickListener(new OnClickListener() {
 65 
 66    @Override
 67    public void onClick(View v) {
 68     Intent i = new Intent(getApplicationContext(), myService.class);
 69     i.putExtra("myprogress", myprogress);
 70     startService(i);//开启服务 完成计数
 71 
 72    }
 73   });
 74   stop.setOnClickListener(new OnClickListener() {
 75 
 76    @Override
 77    public void onClick(View v) {
 78     Intent i = new Intent(getApplicationContext(), myService.class);
 79 
 80     stopService(i);//停止服务,暂停
 81 
 82    }
 83   });
 84   seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
 85 
 86    @Override
 87    public void onStopTrackingTouch(SeekBar seekBar) { // 停止时的事件处理
 88     // TODO Auto-generated method stub
 89     Log.i("Test", "---onStopTrackingTouch");
 90     Intent i = new Intent();
 91     i.setAction("count");
 92     i.putExtra("count", seekBar.getProgress());
 93     sendBroadcast(i);
 94 
 95    }
 96 
 97    @Override
 98    public void onStartTrackingTouch(SeekBar seekBar) {// 开始时的事件处理
 99     // TODO Auto-generated method stub
100     Log.i("Test", "---onStartTrackingTouch");
101    }
102 
103    @Override
104    public void onProgressChanged(SeekBar seekBar, int progress,
105      boolean fromUser) {// 改变时的事件处理
106     tv_current.setText("当前值:" + progress);//拖动是改变
107     Log.i("Test", "---onProgressChanged");
108     myprogress = progress;
109 
110    }
111   });
112 
113  }
114 
115  @Override
116  protected void onDestroy() {
117   // TODO Auto-generated method stub
118   super.onDestroy();
119   unregisterReceiver(receiver);
120   Intent i = new Intent(getApplicationContext(), myService.class);
121 
122   stopService(i);
123 }
}

=============================================================================================

myServer.java

 1 package com.example.aa09_seekbar;
 2 
 3 import android.app.Service;
 4 import android.content.BroadcastReceiver;
 5 import android.content.Context;
 6 import android.content.Intent;
 7 import android.content.IntentFilter;
 8 import android.os.IBinder;
 9 import android.util.Log;
10 
11 public class myService extends Service {
12  private boolean flag = false;
13  private static int count = 0;
14  private MyThead mt;
15  private Thread t;
16 
17  private BroadcastReceiver receiver = new BroadcastReceiver() {
18 
19   @Override
20   public void onReceive(Context context, Intent intent) {
21    int intExtra = intent.getIntExtra("count", 0);
22    count = intExtra;
23 
24   }
25  };
26 
27  @Override
28  public IBinder onBind(Intent intent) {
29   // TODO Auto-generated method stub
30   return null;
31  }
32 
33  @Override
34  public void onCreate() {
35 
36   super.onCreate();
37   mt = new MyThead();// runnable 实现run方法
38   IntentFilter if1 = new IntentFilter();
39   if1.addAction("count");
40   registerReceiver(receiver, if1);
41 
42  }
43 
44  @Override
45  public void onStart(Intent intent, int startId) {
46   // TODO Auto-generated method stub
47   super.onStart(intent, startId);
48   count = intent.getIntExtra("myprogress", count);
49 
50   t = new Thread(mt);
51   t.start();
52 
53  }
54 
55  @Override
56  public void onDestroy() {
57   super.onDestroy();
58   mt.setstop();
59   unregisterReceiver(receiver);
60 
61  }
62 
63  class MyThead implements Runnable {
64   @Override
65   public void run() {
66 
67    while (!flag) {
68 
69     Log.i("Test", "进入while");
70     try {
71      Thread.sleep(1000);
72      count++;
73      if (count == 100) {
74       flag = true;
75      }
76      Intent intent = new Intent();
77      intent.setAction("action");
78      intent.putExtra("count", count);
79      sendBroadcast(intent);// 不停的在发送
80 
81     } catch (InterruptedException e) {
82      // TODO Auto-generated catch block
83      e.printStackTrace();
84     }
85    }
86   }
87 
88   void setstop() {
89    flag = true;
90    }
91  }
92 }

===============================================================================================

AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.aa09_seekbar"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="10"
 9         android:targetSdkVersion="10" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.example.aa09_seekbar.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25 
26         <service android:name="com.example.aa09_seekbar.myService" >
27         </service>
28     </application>
29 
30 </manifest>

 

AndroidManiAndroidManifest.xmlfest.xmlAndroidManifest.xml

m============yService.java

myService.java

myService.java

 

SeekBar

标签:

原文地址:http://www.cnblogs.com/Je-Cortex/p/4830907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!