250x250
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Node
- 나비엔
- 린나이온도조절기
- xcode11
- REACT
- 네모안
- ReactNative
- rasppi3
- todoist
- 경동
- 온도조절기
- Swipes
- Raspberry Pi
- anydo
- 가스요금폭탄
- 에버노트
- 가스비절약
- npm moment
- node.js
- __attribute__
- nodejs
- 난방비절약
- 기름보일러
- debounce
- react-native
- IOT
- EditText
- evernote
- 라온익스
- 온도센서
Archives
- Today
- Total
어허
[Android] NumberPicker 음수 표현방법 본문
728x90
안드로이드
NumberPicker 는 value가 0보다 크거나 같아야 한다는 조건이 있다.
NumberPicker.java 중 일부
public void setMinValue(int minValue) { if (mMinValue == minValue) { return; } if (minValue < 0) { throw new IllegalArgumentException("minValue must be >= 0"); } mMinValue = minValue;
이렇게 되어있다.
이를 해결 하기위해서는
NumberPicker에는 0부터 값을 주고
그 값에 해당하는 String만 "-11" 형식으로 된 String을 집어넣어서 해결하면 된다.
물론 getValue하게되면 음수가 나오는 것은 아니므로, getValue했을때 가져오는 값에서 필요한 만큼 - 를 해서 던져주면 되는 것이다.
샘플코드
int i; String [] stringMin = new String[15]; for ( i = 0; i < stringMin.length; i++) { stringMin[i] = Integer.toString(i-9); } pickerMin = (NumberPicker) findViewById(R.id.numberPickerMin); pickerMin.setMinValue(0); pickerMin.setMaxValue(14); pickerMin.setDisplayedValues(stringMin);
이렇게 하면 stringMin[0]은 "-9", [1]은 "-8" ... 스트링이 들어가게되고
setDisplayedValues(stringMin)에서
NumberPicker의 value0에는 stringMin[0]의 값을 보여주게 된다.
해당값을 리턴해줄 때는 필요한 만큼 (여기서는 9만큼) 빼고 리턴해 주면 된다.
사용자가 -9를 보고 선택을 하면 NumberPicker의 getValue는 0이되고 -9해서 리턴해 주게 되면
부모는 -9라는 int를 리턴받을 수 있다.
public void onDone(View v ) { Intent intent = new Intent(); intent.putExtra("min", pickerMin.getValue()-9); intent.putExtra("max", pickerMax.getValue()-9); setResult(RESULT_OK, intent); finish(); } }
728x90
'개발 > Android' 카테고리의 다른 글
[Android] target SDK 31로 변경 후 푸쉬 에러, FLAG_IMMUTABLE 처리 (0) | 2022.12.01 |
---|---|
Keystore was tampered with, or password was incorrect (0) | 2019.09.16 |
view.getTag() NullPointerException 실수 조심 (0) | 2018.01.22 |
[Android] EditText 키보드 바로 올라오게 하기 (0) | 2016.09.23 |
Comments