12.8 SeekBar和RatingBar

o  SeekBar的主要属性

o  OnSeekBarChangeListener

fragment_main.xml

<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="com.pmplife.progressbar.MainActivity$PlaceholderFragment" >

 

    <SeekBar

        android:id="@+id/firstSeekBar"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>

 

RelativeLayout>

 

 

MainActivity.java

package com.pmplife.progressbar;

 

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBar;

import android.support.v4.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

import android.os.Build;

 

public class MainActivity extends ActionBarActivity {

 

         private SeekBar seekBar;

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.fragment_main);

 

                   seekBar = (SeekBar)findViewById(R.id.firstSeekBar);

                   seekBar.setMax(100);

                   seekBar.setProgress(30);

                   seekBar.setSecondaryProgress(50);

                   SeekBarListener seekBarListener = new SeekBarListener();

                   seekBar.setOnSeekBarChangeListener(seekBarListener);

                  

         }

        

         class SeekBarListener implements OnSeekBarChangeListener

         {

                   @Override

                   /*

                    * seekBar 该对象指的是触发了监听器的SeekBar对象

                    * progress 指的是当前SeekBar的进度

                    * fromUser 进度变化是否由用户引起

                    * @see android.widget.SeekBar.OnSeekBarChangeListener#onProgressChanged(android.widget.SeekBar, int, boolean)

                    */

                   public void onProgressChanged(SeekBar seekBar, int progress,

                                     boolean fromUser) {

                            System.out.println("progress:"+progress+",fromUser"+fromUser);

                           

                   }

                  

                   @Override

                   public void onStartTrackingTouch(SeekBar seekBar) {

                            System.out.println("onStart");

                           

                   }

                  

                   @Override

                   public void onStopTrackingTouch(SeekBar seekBar) {

                            System.out.println("onStop");

                           

                   }

         }

 

         @Override

         public boolean onCreateOptionsMenu(Menu menu) {

 

                   // Inflate the menu; this adds items to the action bar if it is present.

                   getMenuInflater().inflate(R.menu.main, menu);

                   return true;

         }

 

         @Override

         public boolean onOptionsItemSelected(MenuItem item) {

                   // Handle action bar item clicks here. The action bar will

                   // automatically handle clicks on the Home/Up button, so long

                   // as you specify a parent activity in AndroidManifest.xml.

                   int id = item.getItemId();

                   if (id == R.id.action_settings) {

                            return true;

                   }

                   return super.onOptionsItemSelected(item);

         }

 

         /**

          * A placeholder fragment containing a simple view.

          */

         public static class PlaceholderFragment extends Fragment {

 

                   public PlaceholderFragment() {

                   }

 

                   @Override

                   public View onCreateView(LayoutInflater inflater, ViewGroup container,

                                     Bundle savedInstanceState) {

                            View rootView = inflater.inflate(R.layout.fragment_main, container,

                                               false);

                            return rootView;

                   }

         }

 

}

 

 

o  RatingBar的主要属性

o  OnRatingBarChangeListener

fragment_main.xml

<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="com.pmplife.progressbar.MainActivity$PlaceholderFragment" >

 

    <SeekBar

        android:id="@+id/firstSeekBar"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>

   

    <RatingBar

        android:id="@+id/firstRatingBar"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@id/firstSeekBar"

        android:numStars="5"

        android:stepSize="0.5"/>

 

RelativeLayout>

 

 

MainActivity.java

package com.pmplife.progressbar;

 

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBar;

import android.support.v4.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.RatingBar;

import android.widget.RatingBar.OnRatingBarChangeListener;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

import android.os.Build;

 

public class MainActivity extends ActionBarActivity {

 

         private SeekBar seekBar;

         private RatingBar ratingBar;

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.fragment_main);

 

                   seekBar = (SeekBar)findViewById(R.id.firstSeekBar);

                   seekBar.setMax(100);

                   seekBar.setProgress(30);

                   seekBar.setSecondaryProgress(50);

                   SeekBarListener seekBarListener = new SeekBarListener();

                   seekBar.setOnSeekBarChangeListener(seekBarListener);

                  

                   ratingBar=(RatingBar)findViewById(R.id.firstRatingBar);

                   RatingBarListener ratingBarListener = new RatingBarListener();

                   ratingBar.setOnRatingBarChangeListener(ratingBarListener);

                  

         }

        

         class RatingBarListener implements OnRatingBarChangeListener

         {

                   @Override

                   public void onRatingChanged(RatingBar ratingBar, float rating,

                                     boolean fromUser) {

                            System.out.println("rating:"+rating+",fromUser"+fromUser);

                           

                   }

         }

        

         class SeekBarListener implements OnSeekBarChangeListener

         {

                   @Override

                   /*

                    * seekBar 该对象指的是触发了监听器的SeekBar对象

                    * progress 指的是当前SeekBar的进度

                    * fromUser 进度变化是否由用户引起

                    * @see android.widget.SeekBar.OnSeekBarChangeListener#onProgressChanged(android.widget.SeekBar, int, boolean)

                    */

                   public void onProgressChanged(SeekBar seekBar, int progress,

                                     boolean fromUser) {

                            System.out.println("progress:"+progress+",fromUser"+fromUser);

                           

                   }

                  

                   @Override

                   public void onStartTrackingTouch(SeekBar seekBar) {

                            System.out.println("onStart");

                           

                   }

                  

                   @Override

                   public void onStopTrackingTouch(SeekBar seekBar) {

                            System.out.println("onStop");

                           

                   }

         }

 

         @Override

         public boolean onCreateOptionsMenu(Menu menu) {

 

                   // Inflate the menu; this adds items to the action bar if it is present.

                   getMenuInflater().inflate(R.menu.main, menu);

                   return true;

         }

 

         @Override

         public boolean onOptionsItemSelected(MenuItem item) {

                   // Handle action bar item clicks here. The action bar will

                   // automatically handle clicks on the Home/Up button, so long

                   // as you specify a parent activity in AndroidManifest.xml.

                   int id = item.getItemId();

                   if (id == R.id.action_settings) {

                            return true;

                   }

                   return super.onOptionsItemSelected(item);

         }

 

         /**

          * A placeholder fragment containing a simple view.

          */

         public static class PlaceholderFragment extends Fragment {

 

                   public PlaceholderFragment() {

                   }

 

                   @Override

                   public View onCreateView(LayoutInflater inflater, ViewGroup container,

                                     Bundle savedInstanceState) {

                            View rootView = inflater.inflate(R.layout.fragment_main, container,

                                               false);

                            return rootView;

                   }

         }

 

}

 

你可能感兴趣的:(Android)