我想使用數字選擇器來獲取用戶的折扣百分比。一旦用戶輸入銷售價格,我想要一個對話框來詢問折扣百分比。我找不到在對話框中集成numberpicker的方法。如何在對話框中使用數字選擇器
回答
我已經NumberPicker小演示。這可能不完美,但您可以使用和修改相同的內容。
使用自定義對話框並設置號碼選取器。
更多信息@
http://developer.android.com/reference/android/widget/NumberPicker.html
public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener
{
private TextView tv;
static Dialog d ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
Button b = (Button) findViewById(R.id.button11);// on click of button display the dialog
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
show();
}
});
}
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Log.i("value is",""+newVal);
}
public void show()
{
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("NumberPicker");
d.setContentView(R.layout.dialog);
Button b1 = (Button) d.findViewById(R.id.button1);
Button b2 = (Button) d.findViewById(R.id.button2);
final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
np.setMaxValue(100); // max value 100
np.setMinValue(0); // min value 0
np.setWrapSelectorWheel(false);
np.setOnValueChangedListener(this);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
tv.setText(String.valueOf(np.getValue())); //set the value to textview
d.dismiss();
}
});
b2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
d.dismiss(); // dismiss the dialog
}
});
d.show();
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Open" />
</RelativeLayout>
dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/numberPicker1"
android:layout_marginLeft="20dp"
android:layout_marginTop="98dp"
android:layout_toRightOf="@+id/numberPicker1"
android:text="Cancel" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="@+id/numberPicker1"
android:text="Set" />
</RelativeLayout>
快照
我有一個帶虛線的背景圖片,現在我想讓用戶在虛線上繪製,我必須驗證它是否正確?是否可以在畫布上,我該如何處理? – user2841300
@Raghunandan:有沒有什麼辦法可以通過點擊中心值來關閉選擇器。我的意思是,如果我點擊選定的值,那麼numberPicker會消失,有3個值可見?不使用確定/取消按鈕。我嘗試了NumberPicker的onClick方法,但沒有輸出。 :( –
這可以在很多代碼中完成:http://stackoverflow.com/a/40007699/1496122 – k2col
請嘗試以下代碼:
RelativeLayout linearLayout = new RelativeLayout(mContext);
final NumberPicker aNumberPicker = new NumberPicker(mContext);
aNumberPicker.setMaxValue(50);
aNumberPicker.setMinValue(1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
RelativeLayout.LayoutParams numPicerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
numPicerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
linearLayout.setLayoutParams(params);
linearLayout.addView(aNumberPicker,numPicerParams);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Select the number");
alertDialogBuilder.setView(linearLayout);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Log.e("","New Quantity Value : "+ aNumberPicker.getValue());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
這段代碼完全運行正常,我也。我想添加一個數字選擇器。我能夠實現它,但無法格式化它。你能指導我使用java設置等於兩個數字選擇器的寬度。 –
不錯!非常感謝。這使用Android平臺按鈕,所以這是一個要去的地方。爲我節省了很多時間。 –
我選擇了這個答案,因爲用一個xml佈局來擺弄對話似乎太費勁了。當我實現它時,這個實際上看起來更好。 – LordParsley
這是其他的答案
final NumberPicker picker = new NumberPicker(context);
picker.setMinValue(1);
picker.setMaxValue(50);
final FrameLayout layout = new FrameLayout(context);
layout.addView(picker, new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER));
new AlertDialog.Builder(context)
.setView(layout)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something with picker.getValue()
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
它工作的屬性。我正在使用一個EditText,設置focusable爲false,並設置OnClickListener使視圖「單」可點擊。 – brunoramonalmeida
短得多變化創建DialogFragment實施onCreateDialog方法,該方法返回包含NumberPicker視圖警報對話框。 在需要顯示數字選取器對話框並顯示它的活動中實例化對話框片段。
使主機活動實現NumberPicker.OnValueChangeListener接口,以便對話框中的正負按鈕單擊事件處理程序可以通過調用接口onValueChange方法從NumberPicker傳遞當前值。
欲瞭解更多信息,你可以看到http://www.zoftino.com/android-numberpicker-dialog-example。
public class NumberPickerDialog extends DialogFragment {
private NumberPicker.OnValueChangeListener valueChangeListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(20);
numberPicker.setMaxValue(60);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Value");
builder.setMessage("Choose a number :");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
valueChangeListener.onValueChange(numberPicker,
numberPicker.getValue(), numberPicker.getValue());
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
valueChangeListener.onValueChange(numberPicker,
numberPicker.getValue(), numberPicker.getValue());
}
});
builder.setView(numberPicker);
return builder.create();
}
public NumberPicker.OnValueChangeListener getValueChangeListener() {
return valueChangeListener;
}
public void setValueChangeListener(NumberPicker.OnValueChangeListener valueChangeListener) {
this.valueChangeListener = valueChangeListener;
}
}
- 1. 如何把我的自定義數字選擇器在對話框中?
- 2. 如何使計時器的時間選擇器對話框
- 3. 在PropertyGrid中使用自定義顏色選擇器對話框
- 4. 如何在對話框中顯示Android日期選擇器
- 5. 如何在文件選擇器對話框中設置圖標?
- 6. 如何使日期選擇器對話框出現在標籤日期字段
- 7. 如何在VBA中選擇對話框中的選項
- 8. 如何使用fullcalendar選擇回調使用jQuery對話框?
- 9. 如何在OpenGL中使用框選擇高效選擇對象
- 10. 使用SharedPreferences保存複選框對話框中選擇項目
- 11. 在PreferenceActivity中選擇百分比數字的對話框
- 12. 如何在單個首選項選擇中使用兩個對話框
- 13. 如何刪除android時間選擇器對話框中的textview
- 14. 如何使Bootsfaces數據表顯示行選擇對話框
- 15. CKeditor在對話框中動態選擇
- 16. 打開對話框選擇瀏覽器
- 17. 顯示文件選擇器對話框
- 18. 顯示時間選擇器對話框
- 19. Chromecast自定義選擇器對話框
- 20. 多個日期選擇器對話框
- 21. 瞭解顏色選擇器對話框
- 22. Android - 使用動態文本字段使用選擇器對話框共享
- 23. 如何在對話框中使用枚舉對話框
- 24. 如何使用時間選擇器對話框用於其他用途?
- 25. 設計選擇:多選擇對話框?
- 26. 如何在Scheme對話框中選擇Build Action中的Build Configuration?
- 27. 如何使用QT Createor添加文件選擇對話框?
- 28. 如何使用通過文件對話框選擇的文件?
- 29. 如何使用JQueryUI創建單個/多個選擇對話框?
- 30. 如何使用jQuery選擇Modal對話框
谷歌**警告對話框**看看這個【答案】(http://stackoverflow.com/a/27263520/1341526) – jacktrades