Preference で Wheel 入力を使う

Android Wheel プロジェクト http://code.google.com/p/android-wheel/
これを使用して、Preference の項目としてダイアログを使えるか?
結果は、Yes!

f:id:posturan:20160313231350p:plain



DialogPreference を継承するクラスで、android-wheel を実装する。

package uran.wheelsample.time;
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
/**
 * TimeDialogPreference
 */

public class TimeDialogPreference extends DialogPreference{
   private Context context;
   int currentHour;
   int currentMinute;

   public TimeDialogPreference(Context context,AttributeSet attrs){
      super(context,attrs);
      this.context = context;
      // ダイアログのレイアウトリソース
      setDialogLayoutResource(R.layout.preftimecontent);
      setDialogTitle("時刻入力");
      setDialogIcon(android.R.drawable.ic_menu_info_details);
      setPositiveButtonText("SET");
   }
   @Override
   protected void onBindDialogView(View view){

      String timestr = getPersistedString(context.getString(R.string.defaulte_time));
      String[] tary = timestr.split(":");
      currentHour = Integer.parseInt(tary[0]);
      currentMinute = Integer.parseInt(tary[1]);
      WheelView hour = (WheelView)view.findViewById(R.id.hour);
      hour.setViewAdapter(new NumericWheelAdapter(context,0,23,"%02d"));
      hour.setCurrentItem(currentHour);
      hour.addChangingListener(new OnWheelChangedListener(){
         @Override
         public void onChanged(WheelView wheel,int oldValue,int newValue){
            currentHour = newValue;
         }
      });
      WheelView minute = (WheelView)view.findViewById(R.id.minute);
      minute.setViewAdapter(new NumericWheelAdapter(context,0,59,"%02d"));
      minute.setCurrentItem(currentMinute);
      minute.addChangingListener(new OnWheelChangedListener(){
         @Override
         public void onChanged(WheelView wheel,int oldValue,int newValue){
            currentMinute = newValue;
         }
      });

   }
   @Override
   protected void onDialogClosed(boolean positiveResult){

      super.onDialogClosed(positiveResult);
      if (positiveResult){
         // Positive ボタンが押されてCLOSE
         String newtitleStr = String.format("%02d:%02d",currentHour,currentMinute);
         setTitle(newtitleStr);
         persistString(newtitleStr);
      }
   }
}

------- レイアウト preftimecontent.xml の内容、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

      <kankan.wheel.widget.WheelView android:id="@+id/hour"
            android:layout_height="wrap_content"
            android:layout_width="120dp"/>
      <kankan.wheel.widget.WheelView android:id="@+id/minute"
            android:layout_height="wrap_content"
            android:layout_width="120dp"/>

</LinearLayout>


----- res/xml/preference.xml の内容、

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="時刻" >
        <uran.wheelsample.time.TimeDialogPreference
             android:key="time"
             android:selectable="true"
             android:enabled="true"
             android:summary="設定時刻"
          >
        </uran.wheelsample.time.TimeDialogPreference>

    </PreferenceCategory>
</PreferenceScreen>

----- PreferenceActivity のソース

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.preference.PreferenceActivity;
import android.view.View;
import android.widget.Button;
/**
 * PrefTimeActivity
 */

public class PrefTimeActivity extends PreferenceActivity{
   @Override
   protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.preflayout);
      addPreferencesFromResource(R.xml.preference);

      final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
      DialogPreference timePref = (DialogPreference)findPreference("time");
      Context context = getApplicationContext();
      timePref.setTitle(sharedPreferences.getString("time",context.getString(R.string.defaulte_time)));
   }
}