Number Picker!
This commit is contained in:
parent
dee9159a50
commit
eee7cacd71
2 changed files with 152 additions and 0 deletions
app/src/main
|
@ -0,0 +1,108 @@
|
|||
package info.nerull7.mysqlbrowser;
|
||||
|
||||
/**
|
||||
* Created by nerull7 on 18.07.14.
|
||||
*/
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.NumberPicker;
|
||||
|
||||
/**
|
||||
* A dialog that prompts the user for the message deletion limits.
|
||||
*/
|
||||
public class NumberPickerDialog extends AlertDialog implements DialogInterface.OnClickListener {
|
||||
private static final String NUMBER = "number";
|
||||
|
||||
/**
|
||||
* The callback interface used to indicate the user is done filling in
|
||||
* the time (they clicked on the 'Set' button).
|
||||
*/
|
||||
public interface OnNumberSetListener {
|
||||
|
||||
/**
|
||||
* @param number The number that was set.
|
||||
*/
|
||||
void onNumberSet(int number);
|
||||
}
|
||||
|
||||
private final NumberPicker mNumberPicker;
|
||||
private final OnNumberSetListener mCallback;
|
||||
|
||||
/**
|
||||
* @param context Parent.
|
||||
* @param callBack How parent is notified.
|
||||
* @param number The initial number.
|
||||
*/
|
||||
public NumberPickerDialog(Context context,
|
||||
OnNumberSetListener callBack,
|
||||
int number,
|
||||
int rangeMin,
|
||||
int rangeMax,
|
||||
int title) {
|
||||
this(context, AlertDialog.THEME_HOLO_LIGHT, callBack, number, rangeMin, rangeMax, title);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context Parent.
|
||||
* @param theme the theme to apply to this dialog
|
||||
* @param callBack How parent is notified.
|
||||
* @param number The initial number.
|
||||
*/
|
||||
public NumberPickerDialog(Context context,
|
||||
int theme,
|
||||
OnNumberSetListener callBack,
|
||||
int number,
|
||||
int rangeMin,
|
||||
int rangeMax,
|
||||
int title) {
|
||||
super(context, theme);
|
||||
mCallback = callBack;
|
||||
|
||||
setTitle(title);
|
||||
|
||||
setButton(DialogInterface.BUTTON_POSITIVE, context.getText(R.string.set), this);
|
||||
setButton(DialogInterface.BUTTON_NEGATIVE, context.getText(R.string.no),
|
||||
(OnClickListener) null);
|
||||
|
||||
LayoutInflater inflater =
|
||||
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
View view = inflater.inflate(R.layout.number_picker_dialog, null);
|
||||
setView(view);
|
||||
mNumberPicker = (NumberPicker) view.findViewById(R.id.number_picker);
|
||||
|
||||
// initialize state
|
||||
mNumberPicker.setMinValue(rangeMin);
|
||||
mNumberPicker.setMaxValue(rangeMax);
|
||||
mNumberPicker.setValue(number);
|
||||
mNumberPicker.setOnLongPressUpdateInterval(100); // make the repeat rate three times as fast
|
||||
// as normal since the range is so large.
|
||||
mNumberPicker.setWrapSelectorWheel(false); // don't wrap from min->max
|
||||
}
|
||||
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
if (mCallback != null) {
|
||||
mNumberPicker.clearFocus();
|
||||
mCallback.onNumberSet(mNumberPicker.getValue());
|
||||
dialog.dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle onSaveInstanceState() {
|
||||
Bundle state = super.onSaveInstanceState();
|
||||
state.putInt(NUMBER, mNumberPicker.getValue());
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
int number = savedInstanceState.getInt(NUMBER);
|
||||
mNumberPicker.setValue(number);
|
||||
}
|
||||
}
|
44
app/src/main/res/layout/number_picker_dialog.xml
Normal file
44
app/src/main/res/layout/number_picker_dialog.xml
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
/*
|
||||
**
|
||||
** Copyright 2009, Google Inc.
|
||||
**
|
||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||
** you may not use this file except in compliance with the License.
|
||||
** You may obtain a copy of the License at
|
||||
**
|
||||
** http://www.apache.org/licenses/LICENSE-2.0
|
||||
**
|
||||
** Unless required by applicable law or agreed to in writing, software
|
||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
** See the License for the specific language governing permissions and
|
||||
** limitations under the License.
|
||||
*/
|
||||
-->
|
||||
|
||||
<!--
|
||||
This layout file is used by the AlertDialog when displaying a list of items.
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center_horizontal"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<TextView
|
||||
android:gravity="center_horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/pref_messages_to_save"
|
||||
android:padding="1dip" />
|
||||
|
||||
<NumberPicker
|
||||
android:id="@+id/number_picker"
|
||||
android:layout_centerInParent="true"
|
||||
android:layout_width="100dip"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</LinearLayout>
|
Loading…
Add table
Add a link
Reference in a new issue