Home | History | Annotate | Download | only in walt
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.chromium.latency.walt;
     18 
     19 import android.content.Context;
     20 import android.text.Editable;
     21 import android.text.TextWatcher;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.EditText;
     26 import android.widget.NumberPicker;
     27 
     28 public class CustomNumberPicker extends NumberPicker {
     29 
     30     public CustomNumberPicker(Context context, AttributeSet attrs) {
     31         super(context, attrs);
     32     }
     33 
     34     @Override
     35     public void addView(View child) {
     36         super.addView(child);
     37         initEditText(child);
     38     }
     39 
     40     @Override
     41     public void addView(View child, int index) {
     42         super.addView(child, index);
     43         initEditText(child);
     44     }
     45 
     46     @Override
     47     public void addView(View child, int index, ViewGroup.LayoutParams params) {
     48         super.addView(child, index, params);
     49         initEditText(child);
     50     }
     51 
     52     @Override
     53     public void addView(View child, ViewGroup.LayoutParams params) {
     54         super.addView(child, params);
     55         initEditText(child);
     56     }
     57 
     58     @Override
     59     public void addView(View child, int width, int height) {
     60         super.addView(child, width, height);
     61         initEditText(child);
     62     }
     63 
     64     private void initEditText(View view) {
     65         if (view instanceof EditText) {
     66             EditText inputText = (EditText) view;
     67             inputText.addTextChangedListener(new TextWatcher() {
     68                 @Override
     69                 public void onTextChanged(CharSequence s, int start, int before, int count) {
     70                     try {
     71                         CustomNumberPicker.this.setValue(Integer.parseInt(s.toString()));
     72                     } catch (NumberFormatException ignored) {}
     73                 }
     74 
     75                 @Override
     76                 public void afterTextChanged(Editable s) {}
     77 
     78                 @Override
     79                 public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
     80             });
     81         }
     82     }
     83 }
     84