【问题标题】:set values to number pickers in a alert dialog在警报对话框中为数字选择器设置值
【发布时间】:2014-03-02 22:40:20
【问题描述】:

我有一个带有来自 xml 文件的自定义布局的警报对话框。为了让数字选择器工作,我必须为它们设置一些值。我该如何管理?

ShowMap 类中的全局变量:

AlertDialog.Builder alert;  //dialog f. koord.
static Dialog d ;
NumberPicker np1, np2, np3, np4, np5, np6, np7, np8;
String abc[] = new String[] { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" }; 
String zero_to_99[] = new String[100];

showDialog():

    public void showDialog()
{
    final Context context=getApplicationContext();
     //final Dialog d = new Dialog(ShowMap.this);

     //Creating the AlertDialog object
     final AlertDialog.Builder d = new AlertDialog.Builder(this);
     //customDialog.setTitle("Custom Dialog");

     LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
     View view=layoutInflater.inflate(R.layout.dialog_pick_coord,null);

     String txt_title = context.getResources().getString(R.string.txt_head_search_coord);
     d.setTitle(txt_title);

     np1 = (NumberPicker) findViewById(R.id.p1);
     np2 = (NumberPicker) findViewById(R.id.p2);
     np3 = (NumberPicker) findViewById(R.id.p3);
     np4 = (NumberPicker) findViewById(R.id.p4);
     np5 = (NumberPicker) findViewById(R.id.p5);
     np6 = (NumberPicker) findViewById(R.id.p6);
     np7 = (NumberPicker) findViewById(R.id.p7);
     np8 = (NumberPicker) findViewById(R.id.p8);

     //d.setContentView(R.layout.dialog_pick_coord);
     d.setPositiveButton(context.getResources().getString(R.string.Accept), new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
             //Code to read out the pickers and work with the values
         }
     });

     d.setNegativeButton(context.getResources().getString(R.string.Cancel), new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {

         }
     });


     d.setView(view);
     d.show();
}

setAlertDialogValues:

    public void setAlertDialogValues() {
    //Spalte
     np1.setMaxValue(60);   // max value 60
     np1.setMinValue(1);    // min value 1
     np1.setWrapSelectorWheel(false);

     //Zeile
     np2.setMaxValue(25);   // max value Z
     np2.setMinValue(0);    // min value A
     np2.setDisplayedValues( abc );
     np2.setWrapSelectorWheel(false);

     //100km Quadrat 1
     np3.setMaxValue(25);   // max value Z
     np3.setMinValue(0);    // min value A
     np3.setDisplayedValues( abc );
     np3.setWrapSelectorWheel(false);

     //100km Quadrat 2
     np4.setMaxValue(25);   // max value Z
     np4.setMinValue(0);    // min value A
     np4.setDisplayedValues( abc );
     np4.setWrapSelectorWheel(false);

     //Easting xx*
     np5.setMaxValue(99);   // max value 99
     np5.setMinValue(0);    // min value 0
     np5.setDisplayedValues( zero_to_99 );
     np5.setWrapSelectorWheel(false);

     //Easting **x
     np6.setMaxValue(9);    // max value 9
     np6.setMinValue(0);    // min value 0
     np6.setWrapSelectorWheel(false);

     //Northing xx*
     np7.setMaxValue(99);   // max value 99
     np7.setMinValue(0);    // min value 0
     np7.setDisplayedValues( zero_to_99 );
     np7.setWrapSelectorWheel(false);

     //Northing **x
     np8.setMaxValue(9);    // max value 9
     np8.setMinValue(0);    // min value 0
     np8.setWrapSelectorWheel(false);


     np1.setValue(utmCoordElements[0]);
     np2.setValue(utmCoordElements[1]);
     np3.setValue(utmCoordElements[2]);
     np4.setValue(utmCoordElements[3]);
     np5.setValue(utmCoordElements[4]);
     np6.setValue(utmCoordElements[5]);
     np7.setValue(utmCoordElements[6]);
     np8.setValue(utmCoordElements[7]);
}

我应该在哪里调用这个函数?当我在d.show 之后调用它时,我收到了一个错误...

【问题讨论】:

  • 或者我应该在哪里找到ViewById(...)?显示alertdialog时有没有调用方法?
  • 尝试发布错误。
  • 我在 "np1.setMaxValue(60); // 最大值 60" 行中得到一个 nullPointerException

标签: android android-alertdialog


【解决方案1】:

变化:

 np1 = (NumberPicker) findViewById(R.id.p1);
 ....

收件人:

 np1 = (NumberPicker) view.findViewById(R.id.p1);
 ....

【讨论】:

  • 谢谢,现在没有错误,但显示错误。数值不显示,请看我的问题:stackoverflow.com/q/22151739/3147701
  • 这解决了你之前的问题。请关闭此问题。
猜你喜欢
  • 2016-08-28
  • 2021-08-06
  • 1970-01-01
  • 2012-01-01
  • 2012-08-29
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
相关资源
最近更新 更多