Commit abcbc98c authored by Mygod's avatar Mygod

Refine MyNumberPicker

parent 2722b1aa
...@@ -6,30 +6,18 @@ import android.support.v7.preference.DialogPreference ...@@ -6,30 +6,18 @@ import android.support.v7.preference.DialogPreference
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import android.view.ViewGroup.LayoutParams import android.view.ViewGroup.LayoutParams
import android.widget.NumberPicker import android.widget.{EditText, NumberPicker}
import android.widget.EditText
import com.github.shadowsocks.R import com.github.shadowsocks.R
import com.github.shadowsocks.utils.Utils
class MyNumberPicker(private val context: Context, attrs: AttributeSet = null) class MyNumberPicker(private val context: Context, attrs: AttributeSet = null) extends NumberPicker(context, attrs) {
extends NumberPicker(context, attrs) {
override def addView(child: View) {
super.addView(child)
updateView(child)
}
override def addView(child: View, params: LayoutParams) {
super.addView(child, params)
updateView(child)
}
override def addView(child: View, index: Int, params: LayoutParams) { override def addView(child: View, index: Int, params: LayoutParams) {
super.addView(child, index, params) super.addView(child, index, params)
updateView(child) child match {
} case et: EditText => et.setTextSize(Utils.spToPx(context, 6))
def updateView(child: View) { case _ =>
if (child.isInstanceOf[EditText]) {
child.asInstanceOf[EditText].setTextSize(dp2px(6))
} }
} }
def dp2px(dp: Float): Int = (dp*getResources().getDisplayMetrics().density).toInt
} }
class NumberPickerPreference(private val context: Context, attrs: AttributeSet = null) class NumberPickerPreference(private val context: Context, attrs: AttributeSet = null)
......
...@@ -78,7 +78,9 @@ object Utils { ...@@ -78,7 +78,9 @@ object Utils {
} }
def dpToPx(context: Context, dp: Int): Int = def dpToPx(context: Context, dp: Int): Int =
Math.round(dp * (context.getResources.getDisplayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)) Math.round(dp * context.getResources.getDisplayMetrics.density)
def spToPx(context: Context, sp: Int): Int =
Math.round(sp * context.getResources.getDisplayMetrics.scaledDensity)
/* /*
* round or floor depending on whether you are using offsets(floor) or * round or floor depending on whether you are using offsets(floor) or
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment