【问题标题】:How do I achieve having a dropdown list in an edittext? NOTE: I don't want to use spinner [closed]如何在编辑文本中实现下拉列表?注意:我不想使用微调器 [关闭]
【发布时间】:2019-04-08 10:49:14
【问题描述】:

说“我希望用户在填写表单时从下拉列表中进行选择,例如性别”。我可以完全实现所附图像中的内容吗?如果是,怎么做?

【问题讨论】:

  • 为什么不用微调器?
  • 使用微调器并添加与编辑文本相同的背景。你会得到同样的效果。
  • Spinner 是空闲的解决方案,因为它的性别,你不手动输入。 You can change Spinner apperience 或者您可以使用 ListPopUpWindow 而不是 Spinner
  • 您可以使用带有自定义样式的微调器stackoverflow.com/a/37859442/5815673 或根据您对 EditText 的要求设置可绘制对象,并根据您的要求单击打开弹出菜单或任何列表
  • @ADM 这个参考对我来说是完美的......非常感谢

标签: android android-edittext android-spinner


【解决方案1】:

您可以将箭头图像添加为drawable-right/drawable-end。将可聚焦属性和光标可见性设置为 false,然后单击编辑文本打开带有列表的对话框。对于背景使用与电子邮件地址编辑文本相同的背景。 以下是示例 XML 代码

            <EditText
            android:id="@+id/etCustomerType"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_40sdp"
            android:layout_marginStart="@dimen/_15sdp"
            android:layout_marginEnd="@dimen/_15sdp"
            android:backgroundTint="@color/editTextTextColor"
            android:cursorVisible="false"
            android:drawableEnd="@drawable/ic_dropdown"
            android:drawableRight="@drawable/ic_dropdown"
            android:drawablePadding="@dimen/_10sdp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:paddingStart="@dimen/_10sdp"
            android:paddingEnd="@dimen/_10sdp"
            android:textColor="@color/editTextTextColor"
            android:textColorHint="@color/editTextTextColor"
            android:textCursorDrawable="@color/editTextTextColor"
            android:textSize="@dimen/textSizeXSmall" />

【讨论】:

    【解决方案2】:

    我想应该这样做:

    XML 文件应该是:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >   
    <AutoCompleteTextView
        android:id="@+id/sex"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></AutoCompleteTextView>
    </LinearLayout>
    

    MainActivity.java 应该是

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    
    public class MainActivity extends Activity {
    
        String[] sex = { "Male", "Female" };
    
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
                    //Create Array Adapter
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, sex);
                    //Find TextView control
            AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.sex);
            //Set the number of characters the user must type before the drop down list is shown
                    acTextView.setThreshold(1);
                    //Set the adapter
            acTextView.setAdapter(adapter);
        }
    }
    

    【讨论】:

    • 谢谢你,我真的很感激。但我不只是希望用户输入任何内容。我希望用户点击编辑文本并选择他/她的性别
    • 那么你应该使用微调器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多