如何使用数字选择器与对话框 [英] how to use number picker with dialog

查看:173
本文介绍了如何使用数字选择器与对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用数字选择器来获取用户的折扣百分比。一旦用户输入销售价格,我想要一个对话框显示要求折扣百分比。我找不到在对话框中整合数字的方法。

解决方案

我已经做了一个小型演示的NumberPicker。这可能不完美,但您可以使用和修改相同的。



使用自定义对话框并设置数字选择器。



更多信息@



http://developer.android.com/reference/android/widget/NumberPicker.html



public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener
{
private TextView tv;
static对话框d;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv =(TextView)findViewById(R.id.textView1);
按钮b =(Button)findViewById(R.id.button11); //点击按钮显示对话框
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);
按钮b1 =(Button)d.findViewById(R.id.button1);
按钮b2 =(Button)d.findViewById(R.id.button2);
final NumberPicker np =(NumberPicker)d.findViewById(R.id.numberPicker1);
np.setMaxValue(100); //最大值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 ())); //将值设置为textview
d.dismiss();
}
});
b2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v){
d.dismiss(); //关闭对话框
}
});
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 = 主体>

< 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_centerHorizo​​ntal =true
android:text =打开/>

< / 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_centerHorizo​​ntal =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 =取消/>

< 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>

快照




I want to use a number picker for the purpose of getting the discount percentage from the user. once the user enters the sale price, i want a dialog box to appear asking for the discount percentage. I cannot find a way to integrate the numberpicker in the dialog.

解决方案

I have made a small demo of NumberPicker. This may not be perfect but you can use and modify the same.

Use a custom dialog and set the number picker.

More info @

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>

Snap shot

这篇关于如何使用数字选择器与对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆