The seekbar of the development of Android programming using handler Message processing operation method _android

Source: Internet
Author: User

The example of this article describes the development of Android programming Seekbar using handler message processing operations. Share to everyone for your reference, specific as follows:

This case simple implementation progress bar can go, can drag and drop function, below see source code:

Layout file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
 xmlns:tools= "http:// Schemas.android.com/tools "
 android:layout_width=" match_parent "
 android:layout_height=" Match_parent "
 android:paddingbottom= "@dimen/activity_vertical_margin"
 android:paddingleft= "@dimen/activity_ Horizontal_margin "
 android:paddingright=" @dimen/activity_horizontal_margin "
 android:paddingtop=" @dimen /activity_vertical_margin "
 tools:context=". Mainactivity ">
 <seekbar
  android:id=" @+id/seekbar1 "android:layout_width=" Match_parent "
  android:layout_height= "wrap_content"
  android:layout_alignparentleft= "true"
  android:layout_ Alignparenttop= "true"/>
</RelativeLayout>

Java files:

Package Com.example.lession16_seekbar;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.SeekBar;
 public class Mainactivity extends activity {private SeekBar SeekBar;
 private static final int max_progress = 100;
 private int current_progress=10;
 private static final int pro=1;
 Private Handler Handler;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
  SeekBar = (SeekBar) This.findviewbyid (R.ID.SEEKBAR1);
 Seekbar.setmax (max_progress);
      /* New Thread (new Runnable () {@Override public void run () {while (true) {try {thread.sleep (600);
      current_progress+=10;
     Seekbar.setprogress (current_progress);
     catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); }}}). Start (); */handler = newHandler () {@Override public void handlemessage (message msg) {super.handlemessage (msg);
       Switch (msg.what) {case Pro:if (current_progress>=max_progress) {}else{current_progress+=1;
       Seekbar.incrementprogressby (1);
      Handler.sendemptymessagedelayed (PRO, 600);
    } break;
    Default:break;
  }
   }
  };
  current_progress=current_progress>0?current_progress:0;
  Seekbar.setprogress (current_progress);
 Handler.sendemptymessage (PRO); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action Bar if
  It is present.
  Getmenuinflater (). Inflate (R.menu.main, menu);
 return true;

 }
}

Effect Diagram (Automatic walk):

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.