我尝试在android中创建一个水平数字选择器。这似乎是不可能的。另外,我想知道如何调整numberpicker的大小。还有一件事,模拟器中的按钮(+和 - )似乎不够明显。为什么?我怎样才能克服所有这些挑战?
答案 0 :(得分:1)
来源codejeff
动态调整NumberPicker的大小
@Override
NumberPicker
构造函数并设置以下常量
中的数字文字大小
ELEMENT_HEIGHT
=>高度+/-按钮
ELEMENT_WIDTH
=>宽度+/-按钮
MINIMUM
=>最小值NumberPicker
MAXIMUM
=>最大值NumberPicker
TEXT_SIZE
=>NumberPicker
如果你不想搞砸代码。
只需根据 NumberPicker.java 中的要求更改这些值
private final long REPEAT_DELAY = 50;
private final int ELEMENT_HEIGHT = 60;
private final int ELEMENT_WIDTH = ELEMENT_HEIGHT;
private final int MINIMUM = 0;
private final int MAXIMUM = 999;
private final int TEXT_SIZE = 30;` 的 NumberPickerExample.java 强>
public class NumberPickerExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<强> NumberPicker.java 强>
public class NumberPicker extends LinearLayout {
private final long REPEAT_DELAY = 50;
private final int ELEMENT_HEIGHT = 60;
private final int ELEMENT_WIDTH = ELEMENT_HEIGHT; // you're all squares, yo
private final int MINIMUM = 0;
private final int MAXIMUM = 999;
private final int TEXT_SIZE = 30;
public Integer value;
Button decrement;
Button increment;
public EditText valueText;
private Handler repeatUpdateHandler = new Handler();
private boolean autoIncrement = false;
private boolean autoDecrement = false;
class RepetetiveUpdater implements Runnable {
public void run() {
if( autoIncrement ){
increment();
repeatUpdateHandler.postDelayed( new RepetetiveUpdater(), REPEAT_DELAY );
} else if( autoDecrement ){
decrement();
repeatUpdateHandler.postDelayed( new RepetetiveUpdater(), REPEAT_DELAY );
}
}
}
public NumberPicker( Context context, AttributeSet attributeSet ) {
super(context, attributeSet);
this.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
LayoutParams elementParams = new LinearLayout.LayoutParams( ELEMENT_HEIGHT, ELEMENT_WIDTH );
initDecrementButton( context );
initValueEditText( context );
initIncrementButton( context );
if( getOrientation() == VERTICAL ){
addView( increment, elementParams );
addView( valueText, elementParams );
addView( decrement, elementParams );
} else {
addView( decrement, elementParams );
addView( valueText, elementParams );
addView( increment, elementParams );
}
}
private void initIncrementButton( Context context){
increment = new Button( context );
increment.setTextSize( TEXT_SIZE );
increment.setText( "+" );
// Increment once for a click
increment.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
increment();
}
});
increment.setOnLongClickListener(
new View.OnLongClickListener(){
public boolean onLongClick(View arg0) {
autoIncrement = true;
repeatUpdateHandler.post( new RepetetiveUpdater() );
return false;
}
}
);
increment.setOnTouchListener( new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if( event.getAction() == MotionEvent.ACTION_UP && autoIncrement ){
autoIncrement = false;
}
return false;
}
});
}
private void initValueEditText( Context context){
value = new Integer( 0 );
valueText = new EditText( context );
valueText.setTextSize( TEXT_SIZE );
valueText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int arg1, KeyEvent event) {
int backupValue = value;
try {
value = Integer.parseInt( ((EditText)v).getText().toString() );
} catch( NumberFormatException nfe ){
value = backupValue;
}
return false;
}
});
valueText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if( hasFocus ){
((EditText)v).selectAll();
}
}
});
valueText.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
valueText.setText( value.toString() );
valueText.setInputType( InputType.TYPE_CLASS_NUMBER );
}
private void initDecrementButton( Context context){
decrement = new Button( context );
decrement.setTextSize( TEXT_SIZE );
decrement.setText( "-" );
decrement.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
decrement();
}
});
decrement.setOnLongClickListener(
new View.OnLongClickListener(){
public boolean onLongClick(View arg0) {
autoDecrement = true;
repeatUpdateHandler.post( new RepetetiveUpdater() );
return false;
}
}
);
decrement.setOnTouchListener( new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if( event.getAction() == MotionEvent.ACTION_UP && autoDecrement ){
autoDecrement = false;
}
return false;
}
});
}
public void increment(){
if( value < MAXIMUM ){
value = value + 1;
valueText.setText( value.toString() );
}
}
public void decrement(){
if( value > MINIMUM ){
value = value - 1;
valueText.setText( value.toString() );
}
}
public int getValue(){
return value;
}
public void setValue( int value ){
if( value > MAXIMUM ) value = MAXIMUM;
if( value >= 0 ){
this.value = value;
valueText.setText( this.value.toString() );
}
}
}
<强> main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal">
<net.learning.android.control.NumberPicker
android:id="@+id/Picker1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</net.learning.android.control.NumberPicker>
</LinearLayout>
<强>输出强>