2016-01-29 1 views
2

У меня есть два NumberPicker s бок о бок в таблице макета. Слева от 1 до 9 и прокрутки в порядке. Один справа имеет значение от 3 до 5 и не позволит мне изменить значение.Номер Picker застрял на значении

Сборщик борется со своими касаниями и сохраняет значение 5, когда мой код не устанавливает значение.

Эта проблема не происходит на моей S6, но только на ноте 3.

Java-файл

public class Holes extends FragmentActivity { 

//INSTANTIATE VARS// 
TextViewCust title; 
NumberPicker np, nptp; 
Checkbox p1, p2, p3, p4, fw, gir, ud; 
Button n, finish, exit; 
int numPutts = 0, score = 0, girInt = 0, fwInt = 0, udInt = 0, par = 0; 

//DISABLE BACK BUTTON// 
@Override 
public void onBackPressed() 
{ 
    // super.onBackPressed(); // Comment this super call to avoid calling finish() 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_start_round); 

    overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top); 
    //getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_bottom; 

    //holeCountInc(); 

    title = (TextViewCust) findViewById(R.id.Hole1TV); 

    ud = (Checkbox) findViewById(R.id.ud_box); 
    fw = (Checkbox) findViewById(R.id.fairway_box); 
    gir = (Checkbox) findViewById(R.id.gir_box); 

    //CHECKBOX LOGIC 
    p1 = (Checkbox) findViewById(R.id.putt1); 
    p2 = (Checkbox) findViewById(R.id.putt2); 
    p3 = (Checkbox) findViewById(R.id.putt3); 
    p4 = (Checkbox) findViewById(R.id.putt4); 

    setupPuttListeners(); 


    //NUMBER PICKERS 
    nptp = (NumberPicker) findViewById(R.id.scorePickerToPar); 
    nptp.setEnabled(true); 
    nptp.setMinValue(0); 
    nptp.setMaxValue(2); 
    nptp.setDisplayedValues(new String[]{"3", "4", "5"}); 
    nptp.setValue(1); 
    nptp.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 

    setDividerColor(nptp, Color.YELLOW); 

    np = (NumberPicker) findViewById(R.id.scorePicker); 
    np.setMinValue(1); 
    np.setMaxValue(9); 
    np.setValue(1); 
    np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 

    setDividerColor(np, Color.YELLOW); 


    //NEXT HOLE BUTTON// 
    n = (Button) findViewById(R.id.next_button); 

    OnClickListener listnr =new OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      getValues(); 

      //ArrayValues.addTheThings(girInt, score, fwInt, udInt, numPutts, par, 1); 

      numPuttCheck(); 

      addLocalThings(); 
     } 
    }; 
    n.setOnClickListener(listnr); 
    //END NEXT HOLE BUTTON// 


    //EXIT ROUND BUTTON CLICK// 
    exit = (Button) findViewById(R.id.prev_button); 

    OnClickListener exitList =new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(getApplicationContext(), MainActivity.class)); 

     } 
    }; 
    exit.setOnClickListener(exitList); 
    //EXIT BUTTON END// 
} 

public void getValues() { 
    if(p1.isChecked()) { 
     numPutts = 1; 
    } 
    if(p2.isChecked()) { 
     numPutts = 2; 
    } 
    if(p3.isChecked()) { 
     numPutts = 3; 
    } 
    if(p4.isChecked()) { 
     numPutts = 4; 
    } 
    score = np.getValue(); 

    par = nptp.getValue(); 

    //1 means checked 
    girInt = gir.checker(); 

    fwInt = fw.checker(); 

    udInt = ud.checker(); 
} 

public void setupPuttListeners() { 
    p1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //is chkIos checked? 
      if (((CheckBox) v).isChecked()) { 
       //Case 1 
       p2.setChecked(false); 
       p3.setChecked(false); 
       p4.setChecked(false); 

       np.setEnabled(false); 
       nptp.setEnabled(false); 

       if (np.getValue() - (nptp.getValue() + 3) >= 0) 
        gir.setChecked(false); 
       else if(np.getValue() - (nptp.getValue() + 3) <= -1) 
        gir.setChecked(true); 

      } else { 
       gir.setChecked(false); 
       np.setEnabled(true); 
       nptp.setEnabled(true); 
       return; 
      } 

     } 
    }); 

    p2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //is chkIos checked? 
      if (((CheckBox) v).isChecked()) { 
       //Case 1 
       p1.setChecked(false); 
       p3.setChecked(false); 
       p4.setChecked(false); 

       np.setEnabled(false); 
       nptp.setEnabled(false); 

       if (np.getValue() == (nptp.getValue() + 3) || (np.getValue() - (nptp.getValue()+3) == -1)) 
        gir.setChecked(true); 
       else 
        gir.setChecked(false); 
      } else { 
       gir.setChecked(false); 
       np.setEnabled(true); 
       nptp.setEnabled(true); 
       return; 
      } 

     } 
    }); 

    p3.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //is chkIos checked? 
      if (((CheckBox) v).isChecked()) { 
       //Case 1 
       p2.setChecked(false); 
       p1.setChecked(false); 
       p4.setChecked(false); 

       np.setEnabled(false); 
       nptp.setEnabled(false); 

       if (np.getValue() - (nptp.getValue() + 3) <= 0) 
        gir.setChecked(false); 
       else if(np.getValue() - (nptp.getValue() + 3) == 1) 
        gir.setChecked(true); 

      } else { 
       //case 2 
       np.setEnabled(true); 
       nptp.setEnabled(true); 
       return; 
      } 

     } 
    }); 

    p4.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //is chkIos checked? 
      if (((CheckBox) v).isChecked()) { 
       //Case 1 
       p2.setChecked(false); 
       p3.setChecked(false); 
       p1.setChecked(false); 
       gir.setChecked(false); 

       np.setEnabled(false); 
       nptp.setEnabled(false); 
      } else { 
       //case 2 
       np.setEnabled(true); 
       nptp.setEnabled(true); 
       return; 
      } 

     } 
    }); //END CHECKBOX SECTION 
} 

public void askForCourse() { 
    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Where are you playing?"); 
    builder.setCancelable(false); 
    // Set up the input 
    final EditText input = new EditText(this); 
    // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text 
    input.setInputType(InputType.TYPE_CLASS_TEXT); 
    builder.setView(input); 

    // Set up the buttons 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     } 
    }); 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      startActivity(new Intent(getApplicationContext(), MainActivity.class)); 
     } 
    }); 

    final AlertDialog alertDialog = builder.create(); 
    alertDialog.show(); 
    alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Boolean wantToCloseDialog = (input.getText().toString().trim().isEmpty()); 
      // if EditText is empty disable closing on possitive button 
      if (!wantToCloseDialog) { 
       ArrayValues.course = input.getText().toString(); 
       alertDialog.dismiss(); 
      } else 
       Toast.makeText(getApplicationContext(), "Please enter a course name", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

public int parseHole(String s) { 
    int x = 0; 
    Character l, m; 
    for(int i = 0; i < s.length(); i++) { 
     if(Character.isDigit(s.charAt(i))) { 
      l = s.charAt(i); 

      if(Character.isDigit(s.charAt(i + 1))) { 
       m = s.charAt(i + 1); 
       String d = l.toString() + m.toString(); 
       Log.d("D", "" + d); 
       x = Integer.parseInt(d); 
       break; 
      } 

      x = Character.getNumericValue(l); 
      break; 
     } 
     else x = 0; 
    } 
    Log.d("String", "" + s); 
    return x; 
} 

public void setTitleText(int i) { 
    title.setText("Hole " + i); 
} 

public void addLocalThings() { 
    ArrayValues.addTheThings(girInt, score, fwInt, udInt, numPutts, par, 0); 
} 

public void numPuttCheck() { 

    if(numPutts == 0) { 
     Toast.makeText(getApplicationContext(), "Nice Hole Out!", 
       Toast.LENGTH_SHORT).show(); 
     startActivity(new Intent(getApplicationContext(), Hole1V2.class)); 
    } 
    else 
     startActivity(new Intent(getApplicationContext(), Hole1V2.class)); 
} 

private void setDividerColor(NumberPicker picker, int color) { 

    java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields(); 
    for (java.lang.reflect.Field pf : pickerFields) { 
     if (pf.getName().equals("mSelectionDivider")) { 
      pf.setAccessible(true); 
      try { 
       ColorDrawable colorDrawable = new ColorDrawable(color); 
       pf.set(picker, colorDrawable); 
      } catch (IllegalArgumentException e) { 
       e.printStackTrace(); 
      } catch (Resources.NotFoundException e) { 
       e.printStackTrace(); 
      } 
      catch (IllegalAccessException e) { 
       e.printStackTrace(); 
      } 
      break; 
     } 
    } 
} 

}

XML-файл

<TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:background="@color/white50"> 

     <com.ryancase.golf.TextViewCust 
      android:layout_height="wrap_content" 
      android:text="@string/h1" 
      android:theme="@style/TitleText" 
      android:textSize="30pt" 
      android:textColor="@color/newGreen" 
      android:id="@+id/Hole1TV" 
      android:layout_gravity="center"/> 

    </TableRow> 


    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_height="50px" 
      android:layout_width="wrap_content"/> 

    </TableRow> 

    <!-- SCORE PICKER ROW--> 
    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:theme="@style/NumberPicker"> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="2"/> 

     <NumberPicker 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="6" 
      android:id="@+id/scorePicker" 
      android:textAlignment="center" 
      android:gravity="center_vertical" /> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <NumberPicker 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="6" 
      android:id="@+id/scorePickerToPar" 
      android:textAlignment="center" 
      android:gravity="center_vertical" /> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="2"/> 

    </TableRow> 



    <TableRow> 
     <TextView 
      android:text="PUTTS:" 
      android:textSize="75px" 
      android:textStyle="bold" 
      android:textColor="@color/grey"/> 
    </TableRow> 

    <TableRow> <!-- BEGINNING OF PUTT ROW --> 
     <com.ryancase.golf.Checkbox 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:paddingLeft="30px" 
      android:text="1" 
      android:layout_weight="4" 
      android:textStyle="bold" 
      android:textAlignment="center" 
      android:textSize="75px" 
      android:id="@+id/putt1"/> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <com.ryancase.golf.Checkbox 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:paddingLeft="30px" 
      android:text="2" 
      android:textAlignment="gravity" 
      android:layout_weight="4" 
      android:textStyle="bold" 
      android:textSize="75px" 
      android:id="@+id/putt2"/> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <com.ryancase.golf.Checkbox 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:paddingLeft="30px" 
      android:text="3" 
      android:textAlignment="gravity" 
      android:layout_weight="4" 
      android:textStyle="bold" 

      android:textSize="75px" 
      android:id="@+id/putt3"/> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <com.ryancase.golf.Checkbox 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:paddingLeft="30px" 
      android:text="4" 
      android:layout_weight="4" 
      android:textStyle="bold" 
      android:textSize="75px" 
      android:id="@+id/putt4"/> 
    </TableRow> <!-- END OF PUTT ROW --> 

    <!-- SPACER ROW --> 
    <TableRow android:layout_marginBottom="10dp"> 
     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" /> 
    </TableRow> 
    <!-- SPACER ROW END --> 

    <!-- BEGINNING OF GIR ROW --> 
    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <com.ryancase.golf.Checkbox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:paddingLeft="30px" 
      android:text="Fairway" 
      android:layout_weight="3" 
      android:textStyle="bold" 
      android:textSize="90px" 
      android:gravity="center" 
      android:id="@+id/fairway_box"/> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <com.ryancase.golf.Checkbox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:paddingLeft="30px" 
      android:text="Up/Down" 
      android:textAlignment="gravity" 
      android:layout_weight="3" 
      android:textStyle="bold" 
      android:textSize="90px" 
      android:gravity="center" 
      android:id="@+id/ud_box"/> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <com.ryancase.golf.Checkbox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:paddingLeft="30px" 
      android:text="GIR" 
      android:textAlignment="gravity" 
      android:layout_weight="3" 
      android:textStyle="bold" 
      android:textSize="90px" 
      android:gravity="center" 
      android:id="@+id/gir_box"/> 
    </TableRow> <!-- END OF GIR FW ROW --> 


    <TableRow> 

    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Exit Round" 
      android:layout_marginTop="200px" 
      android:id="@+id/prev_button" 
      android:layout_weight="1" 
      android:textColor="@color/white" 
      android:background="@drawable/next_bg" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_weight="1" 
      android:layout_height="wrap_content"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Next Hole" 
      android:layout_marginTop="200px" 
      android:id="@+id/next_button" 
      android:layout_weight="1" 
      android:textColor="@color/white" 
      android:background="@drawable/next_bg" /> 

    </TableRow> 

</TableLayout> 

Второй NumberPicker не показывает здесь ИКА почему Он имеет такой же точный код XML, кроме идентификатора

UPDATE:

Я свел к тому, что один на праве имеет только три значения в нем. Почему это может вызвать проблемы?

+0

и больше нигде в коде вы трогай сборщика? – njzk2

+0

Метод getValue сборщиков называется –

ответ

0

Удалить nptp.setMinValue(0); и nptp.setMaxValue(2);.

Ваши NumberPicker значения: 3, 4 и 5. Поэтому установка диапазона между 0 и 2 не имеет смысла.

Изменить код на:

nptp = (NumberPicker) findViewById(R.id.scorePickerToPar); 
nptp.setDisplayedValues(new String[]{"3", "4", "5"}); 
nptp.setMinValue(3); 
nptp.setMaxValue(5); 

Или просто

nptp = (NumberPicker) findViewById(R.id.scorePickerToPar); 
nptp.setMinValue(3); 
nptp.setMaxValue(5); 
+0

Удаление диапазона, и только с помощью setDisplayedValues ​​отображается только 3 в списке выбора номеров. Установка минимального и максимального значений на 3 и 5 получает правильные значения, но выбор номера по-прежнему не позволяет мне прокручивать числа –

+0

У вас есть право, я обновил свое первое предложение. Но я только что протестировал второе предложение, и оно работает для меня. Можете ли вы разместить весь код java и xml? возможно, что-то не так. – Rami

+0

Я отправил полный код –

Смежные вопросы