Advertisement
Guest User

NumberPicker.java

a guest
Oct 15th, 2011
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.84 KB | None | 0 0
  1. ( Code from http://www.technologichron.net/?p=42, with modifications. )
  2.  
  3.  
  4.  
  5.  
  6. /*
  7.  * Copyright (c) 2010, Jeffrey F. Cole
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  *
  13.  *  Redistributions of source code must retain the above copyright notice, this
  14.  *  list of conditions and the following disclaimer.
  15.  *
  16.  *  Redistributions in binary form must reproduce the above copyright notice,
  17.  *  this list of conditions and the following disclaimer in the documentation
  18.  *  and/or other materials provided with the distribution.
  19.  *
  20.  *  Neither the name of the technologichron.net nor the names of its contributors
  21.  *  may be used to endorse or promote products derived from this software
  22.  *  without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  28.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34.  * POSSIBILITY OF SUCH DAMAGE.
  35.  */
  36.  
  37. package com.android.GissaSiffran;
  38.  
  39. import android.content.Context;
  40. import android.os.Handler;
  41. import android.text.InputType;
  42. import android.util.AttributeSet;
  43. import android.util.Log;
  44. import android.view.Gravity;
  45. import android.view.KeyEvent;
  46. import android.view.MotionEvent;
  47. import android.view.View;
  48. import android.widget.Button;
  49. import android.widget.EditText;
  50. import android.widget.LinearLayout;
  51.  
  52. /**
  53.  * A simple layout group that provides a numeric text area with two buttons to
  54.  * increment or decrement the value in the text area. Holding either button
  55.  * will auto increment the value up or down appropriately.
  56.  *
  57.  * @author Jeffrey F. Cole
  58.  *
  59.  */
  60. public class NumberPicker extends LinearLayout {
  61.  
  62.     private final long REPEAT_DELAY = 50;
  63.    
  64.     private final int ELEMENT_HEIGHT   = 70;
  65.     private final int ELEMENT_WIDTH    =  ELEMENT_HEIGHT; // you're all squares, yo
  66.     private final int NUM_FIELD_HEIGHT = 70;    //La till 2011-10-13
  67.     private final int NUM_FIELD_WIDTH  = 100;   //La till 2011-10-13
  68.    
  69.     private final int MINIMUM = 0;
  70.     private final int MAXIMUM = 10000;
  71.    
  72.     public Integer value;   //WANT THIS VALUE!!!
  73.    
  74.     Button decrement;
  75.     Button increment;
  76.     public EditText valueText;
  77.    
  78.     private Handler repeatUpdateHandler = new Handler();
  79.    
  80.     private boolean autoIncrement = false;
  81.     private boolean autoDecrement = false;
  82.  
  83.     /**
  84.      * This little guy handles the auto part of the auto incrementing feature.
  85.      * In doing so it instantiates itself. There has to be a pattern name for
  86.      * that...
  87.      *
  88.      * @author Jeffrey F. Cole
  89.      *
  90.      */
  91.     class RepetetiveUpdater implements Runnable {
  92.         public void run() {
  93.             if( autoIncrement ){
  94.                 increment();
  95.                 repeatUpdateHandler.postDelayed( new RepetetiveUpdater(), REPEAT_DELAY );
  96.             } else if( autoDecrement ){
  97.                 decrement();
  98.                 repeatUpdateHandler.postDelayed( new RepetetiveUpdater(), REPEAT_DELAY );
  99.             }
  100.         }
  101.     }
  102.    
  103.     public NumberPicker( Context context, AttributeSet attributeSet) {
  104.         super(context, attributeSet);
  105.        
  106.         this.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
  107.         LayoutParams elementParams = new LinearLayout.LayoutParams( ELEMENT_WIDTH, ELEMENT_HEIGHT ); //Byte plats på WIDTH och HEIGHT
  108.        
  109.         LayoutParams elementNumFieldParams = new LinearLayout.LayoutParams( NUM_FIELD_WIDTH, NUM_FIELD_HEIGHT );//La till 2011-10-13
  110.        
  111.         // init the individual elements
  112.         initDecrementButton( context );
  113.         initValueEditText( context );
  114.         initIncrementButton( context );
  115.        
  116.         // Can be configured to be vertical or horizontal
  117.         // Thanks for the help, LinearLayout!  
  118.         if( getOrientation() == VERTICAL ){
  119.             addView( increment, elementParams );
  120.             addView( valueText, elementNumFieldParams ); //Ändrade 2011-10-13
  121.             addView( decrement, elementParams );
  122.         } else {
  123.             addView( decrement, elementParams );
  124.             addView( valueText, elementNumFieldParams ); //Ändrade 2011-10-13
  125.             addView( increment, elementParams );
  126.         }
  127.     }
  128.    
  129.     private void initIncrementButton( Context context){
  130.         increment = new Button( context );
  131.         increment.setTextSize( 25 );
  132.         increment.setText( "+" );
  133.        
  134.         // Increment once for a click
  135.         increment.setOnClickListener(new View.OnClickListener() {
  136.             public void onClick(View v) {
  137.                 increment();
  138.             }
  139.         });
  140.        
  141.         // Auto increment for a long click
  142.         increment.setOnLongClickListener(
  143.                 new View.OnLongClickListener(){
  144.                     public boolean onLongClick(View arg0) {
  145.                         autoIncrement = true;
  146.                         repeatUpdateHandler.post( new RepetetiveUpdater() );
  147.                         return false;
  148.                     }
  149.                 }
  150.         );
  151.        
  152.         // When the button is released, if we're auto incrementing, stop
  153.         increment.setOnTouchListener( new View.OnTouchListener() {
  154.             public boolean onTouch(View v, MotionEvent event) {
  155.                 if( event.getAction() == MotionEvent.ACTION_UP && autoIncrement ){
  156.                     autoIncrement = false;
  157.                 }
  158.                 return false;
  159.             }
  160.         });
  161.     }
  162.    
  163.     private void initValueEditText( Context context){
  164.        
  165.         value = new Integer( 0 );
  166.        
  167.         valueText = new EditText( context );
  168.         valueText.setId(R.id.number_picker_value);
  169.         valueText.setTextSize(25);
  170.        
  171.         Log.d("****1*****", "initValueEditText!!!!!");
  172.         Log.d("****1*****", "Context ="+ String.valueOf(context));
  173.  
  174.        
  175.         // Since we're a number that gets affected by the button, we need to be
  176.         // ready to change the numeric value with a simple ++/--, so whenever
  177.         // the value is changed with a keyboard, convert that text value to a
  178.         // number. We can set the text area to only allow numeric input, but
  179.         // even so, a carriage return can get hacked through. To prevent this
  180.         // little quirk from causing a crash, store the value of the internal
  181.         // number before attempting to parse the changed value in the text area
  182.         // so we can revert to that in case the text change causes an invalid
  183.         // number
  184.         valueText.setOnKeyListener(new View.OnKeyListener() {
  185.             public boolean onKey(View v, int arg1, KeyEvent event) {
  186.                 int backupValue = value;
  187.                 try {
  188.                     value = Integer.parseInt( ((EditText)v).getText().toString() );
  189.                 } catch( NumberFormatException nfe ){
  190.                     value = backupValue;
  191.                 }
  192.                 return false;
  193.             }
  194.         });
  195.        
  196.         // Highlight the number when we get focus
  197.         valueText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  198.             public void onFocusChange(View v, boolean hasFocus) {
  199.                 if( hasFocus ){
  200.                     ((EditText)v).selectAll();
  201.                 }
  202.             }
  203.         });
  204.         valueText.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
  205.         valueText.setText( value.toString() );
  206.         valueText.setInputType( InputType.TYPE_CLASS_NUMBER );
  207.     }
  208.    
  209.     private void initDecrementButton( Context context){
  210.         decrement = new Button( context );
  211.         decrement.setTextSize( 25 );
  212.         decrement.setText( "-" );
  213.        
  214.  
  215.         // Decrement once for a click
  216.         decrement.setOnClickListener(new View.OnClickListener() {
  217.             public void onClick(View v) {
  218.                 decrement();
  219.             }
  220.         });
  221.        
  222.  
  223.         // Auto Decrement for a long click
  224.         decrement.setOnLongClickListener(
  225.                 new View.OnLongClickListener(){
  226.                     public boolean onLongClick(View arg0) {
  227.                         autoDecrement = true;
  228.                         repeatUpdateHandler.post( new RepetetiveUpdater() );
  229.                         return false;
  230.                     }
  231.                 }
  232.         );
  233.        
  234.         // When the button is released, if we're auto decrementing, stop
  235.         decrement.setOnTouchListener( new View.OnTouchListener() {
  236.             public boolean onTouch(View v, MotionEvent event) {
  237.                 if( event.getAction() == MotionEvent.ACTION_UP && autoDecrement ){
  238.                     autoDecrement = false;
  239.                 }
  240.                 return false;
  241.             }
  242.         });
  243.     }
  244.    
  245.     public void increment() {
  246.         if( value < MAXIMUM ){
  247.             value = value + 1;
  248.             valueText.setText( value.toString() );
  249.            
  250.             Log.d("****1*****", "++!");
  251.             Log.d("****1*****", "Value = "+ value);
  252.         }
  253.     }
  254.  
  255.     public void decrement() {
  256.         if( value > MINIMUM ){
  257.             value = value - 1;
  258.             valueText.setText( value.toString() );
  259.            
  260.             Log.d("****1*****", "--!");
  261.             Log.d("****1*****", "Value = "+ value);
  262.         }
  263.     }
  264.    
  265.     public int getValue(){
  266.         return value;
  267.     }
  268.    
  269.     public void setValue( int value ){
  270.         if( value > MAXIMUM ) value = MAXIMUM;
  271.         if( value >= 0 ){
  272.             this.value = value;
  273.             valueText.setText( this.value.toString() );
  274.         }
  275.     }
  276.    
  277. }
  278.  
  279.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement