我对Android非常陌生我快速接受它但速度不够快。我的应用程序上有数字选择器小部件。窗口小部件加载在页面上但有两个问题。第一个是在小部件框内,没有显示预期为0的位置。第二个问题是,如果按下+或 - 按钮,应用程序崩溃。我的设备是Android 2.3.4(HTC Wildfire),我正在使用eclipse。
这是我的java文件
public class NumberPickerActivity extends Activity {
private Button upButton;
private Button downButton;
private EditText editText;
private int uprange = 20;
private int downrange = 0;
private int values = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
upButton = (Button) findViewById(R.id.upButton);
downButton = (Button) findViewById(R.id.downButton);
editText = (EditText) findViewById(R.id.numberEditText);
upButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
downButton.setBackgroundResource(R.drawable.timepicker_down_normal);
upButton.setBackgroundResource(R.drawable.timepicker_up_pressed);
if (values >= downrange && values <= uprange)
values++;
if (values > uprange)
values = downrange;
editText.setText("" + values);
}
});
downButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
downButton .setBackgroundResource(R.drawable.timepicker_down_pressed);
upButton.setBackgroundResource(R.drawable.timepicker_up_normal);
if (values >= downrange && values <= uprange)
values--;
if (values < downrange)
values = uprange;
editText.setText(values + "");
}
});
}
}
这是我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="horizontal" >
<Button
android:id="@+id/upButton"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_toLeftOf="@+id/numberEditText"
android:background="@drawable/timepicker_up_normal"
android:onClick="onupButtonPress"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_marginRight="43dp"
android:text="Welcome whatever your name is"
android:textSize="24sp" />
<Button
android:id="@+id/downButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/numberEditText"
android:layout_alignLeft="@+id/upButton"
android:layout_alignRight="@+id/upButton"
android:layout_below="@+id/upButton"
android:background="@drawable/timepicker_down_normal"
android:onClick="ondownButtonPress"/>
<EditText
android:id="@+id/numberEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/scanButton"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/timepicker_input_selected"
android:ems="10"
android:gravity="center"
android:inputType="number" />
</RelativeLayout>