From 4be4a8156a013e24fcf88616ba4ad7d2e13aa808 Mon Sep 17 00:00:00 2001 From: eehouse Date: Thu, 27 May 2010 02:57:35 +0000 Subject: [PATCH] wire new edittext to seekbar and vice-versa, including hack boolean to prevent loop that resets the cursor to the 0 position on every keystroke. --- .../android/xw4/EditColorPreference.java | 74 ++++++++++++++++--- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/EditColorPreference.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/EditColorPreference.java index ebe9e520e..e33eaf313 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/EditColorPreference.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/EditColorPreference.java @@ -27,33 +27,48 @@ import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.widget.SeekBar; +import android.widget.EditText; import android.app.Dialog; import android.content.SharedPreferences; import android.app.AlertDialog; - +import android.text.TextWatcher; +import android.text.Editable; import junit.framework.Assert; public class EditColorPreference extends DialogPreference { private Context m_context; private int m_curColor; + // m_updateText: prevent loop that resets edittext cursor + private boolean m_updateText = true; + private static final int m_seekbarIds[] = { R.id.seek_red, R.id.seek_green, + R.id.seek_blue }; + private static final int m_editIds[] = { R.id.edit_red, R.id.edit_green, + R.id.edit_blue }; private class SBCL implements SeekBar.OnSeekBarChangeListener { - int m_shift; + int m_index; View m_sample; - public SBCL( View parent, int shift ) + EditText m_editTxt; + public SBCL( View parent, EditText editTxt, int indx ) { - m_shift = shift; + m_index = indx; m_sample = parent.findViewById( R.id.color_edit_sample ); + m_editTxt = editTxt; } public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser ) { + if ( m_updateText ) { + m_editTxt.setText( String.format( "%d", progress ) ); + } + + int shift = 16 - (m_index * 8); // mask out the byte we're changing - int color = m_curColor & ~(0xFF << m_shift); + int color = m_curColor & ~(0xFF << shift); // add in the new version of the byte - color |= progress << m_shift; + color |= progress << shift; m_curColor = color; m_sample.setBackgroundColor( m_curColor ); } @@ -63,6 +78,32 @@ public class EditColorPreference extends DialogPreference { public void onStopTrackingTouch( SeekBar seekBar ) {} } + private class TCL implements TextWatcher { + private SeekBar m_seekBar; + public TCL( SeekBar seekBar ) { m_seekBar = seekBar; } + public void afterTextChanged( Editable s ) + { + } + + public void beforeTextChanged( CharSequence s, int st, int cnt, int a ) + { + } + + public void onTextChanged( CharSequence s, int start, + int before, int count ) + { + int val; + try { + val = Integer.parseInt( s.toString() ); + } catch ( java.lang.NumberFormatException nfe ) { + val = 0; + } + m_updateText = false; // don't call me recursively inside seekbar + m_seekBar.setProgress( val ); + m_updateText = true; + } + } + public EditColorPreference( Context context, AttributeSet attrs ) { super( context, attrs ); @@ -96,9 +137,9 @@ public class EditColorPreference extends DialogPreference { protected void onBindDialogView( View view ) { m_curColor = getPersistedColor(); - setOneByte( view, R.id.seek_red, 16 ); - setOneByte( view, R.id.seek_green, 8 ); - setOneByte( view, R.id.seek_blue, 0 ); + setOneByte( view, 0 ); + setOneByte( view, 1 ); + setOneByte( view, 2 ); View sample = (View)view.findViewById( R.id.color_edit_sample ); sample.setBackgroundColor( m_curColor ); @@ -124,14 +165,23 @@ public class EditColorPreference extends DialogPreference { super.onPrepareDialogBuilder( builder ); } - private void setOneByte( View parent, int id, int shift ) + private void setOneByte( View parent, int indx ) { + int shift = 16 - (indx*8); int byt = (m_curColor >> shift) & 0xFF; - SeekBar seekbar = (SeekBar)parent.findViewById( id ); + SeekBar seekbar = (SeekBar)parent.findViewById( m_seekbarIds[indx] ); + EditText edittext = (EditText)parent.findViewById( m_editIds[indx] ); + if ( null != seekbar ) { seekbar.setProgress( byt ); - seekbar.setOnSeekBarChangeListener( new SBCL( parent, shift ) ); + seekbar.setOnSeekBarChangeListener( new SBCL( parent, edittext, + indx ) ); + } + + if ( null != edittext ) { + edittext.setText( String.format( "%d", byt ) ); + edittext.addTextChangedListener( new TCL( seekbar ) ); } }