【问题标题】:How to add Up and Down arrows to android EditText in Android如何在 Android 中向 android EditText 添加向上和向下箭头
【发布时间】:2020-10-28 17:38:39
【问题描述】:

如何像这样向 editText 添加箭头?

有没有办法在 EditText 中显示number picker

【问题讨论】:

  • 为此使用ImageButton
  • 请看https://stackoverflow.com/questions/17944061/how-to-use-number-picker-with-dialog

标签: android


【解决方案1】:

在您的 XML 文件中

<NumberPicker
        android:id="@+id/numberPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

您可以在数字选择器对象上调用 setOnValueChangedListener 方法,将值更改侦听器添加到 NumberPicker。 公共类 MainActivity 扩展 AppCompatActivity {

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

       NumberPicker np = findViewById(R.id.numberPicker);

       np.setMinValue(2);
       np.setMaxValue(20);

       np.setOnValueChangedListener(onValueChangeListener);
    }

    NumberPicker.OnValueChangeListener onValueChangeListener =
            new     NumberPicker.OnValueChangeListener(){
        @Override
        public void onValueChange(NumberPicker numberPicker, int i, int i1) {
            Toast.makeText(MainActivity.this,
                    "selected number "+numberPicker.getValue(), Toast.LENGTH_SHORT);
        }
    };
}

【讨论】:

    【解决方案2】:

    制作一个由 EditText(或 TextView)和 2 个 ImageButton 组成的自定义视图。这应该可以帮助您开始:

    1. 创建类:

       public class MyNumberPickerView extends ConstraintLayout {
      
           private ImageButton incrementButton;
           private ImageButton decrementButton;
           private EditText numberEditText;
      
           public MyNumberPickerView(Context context) {
               super(context);
           }
      
           public MyNumberPickerView(Context context, AttributeSet attrs) {
               super(context, attrs);
           }
      
           public MyNumberPickerView(Context context, AttributeSet attrs, int defStyleAttr) {
               super(context, attrs, defStyleAttr);
           }
      
           @Override
           protected void onFinishInflate() {
               super.onFinishInflate();
               incrementButton = findViewById(R.id.increment);
               decrementButton = findViewById(R.id.decrement);
               numberEditText = findViewById(R.id.number);
           }
      
           // expose public functions for listeners, setting a number, etc.
       }
      
    2. 创建布局:

       <?xml version="1.0" encoding="utf-8"?>
       <com.company.app.ui.common.MyNumberPickerView xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
      
           <ImageButton
               android:id="@+id/increment"
               style="@style/Widget.AppCompat.Button.Borderless"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/ic_arrow_up"
               app:layout_constraintBottom_toTopOf="@+id/number"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintTop_toTopOf="parent"
               app:layout_constraintVertical_chainStyle="packed" />
      
           <EditText
               android:id="@+id/number"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:background="@android:color/transparent"
               android:gravity="center"
               android:inputType="number"
               android:selectAllOnFocus="true"
               app:layout_constraintBottom_toTopOf="@id/decrement"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintTop_toBottomOf="@id/increment"
               tools:text="99" />
      
           <ImageButton
               android:id="@+id/decrement"
               style="@style/Widget.AppCompat.Button.Borderless"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/ic_arrow_down"
               app:layout_constraintBottom_toBottomOf="parent"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintTop_toBottomOf="@id/number" />
      
       </com.company.app.ui.common.MyNumberPickerView>
      
    1. 在其他布局中包含您的自定义视图,例如:

       <include android:id="@+id/numberpicker"                     
           layout="@layout/view_mynumberpicker"        
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
      
    2. 并在充气后使用:

       MyNumberPickerView myNumberPickerView = findViewById(R.id.numberpicker);
      

    【讨论】: