单击EditText打开微调器

时间:2017-12-06 03:26:44

标签: android spinner android-spinner

很抱歉打扰你们,但我一直在扼杀这个工作。

我想要它做什么:

EditText显示提示。

用户点击EditText,打开微调器并选择他们的性别。然后变成一个字符串并放入EditText(性别)

它实际上在做什么: “男性”我的微调器中的第一个元素已经放入我的EditText中,然后用户甚至点击它。(我从未看到我的提示:“性别”)....当我尝试时,微调器根本不会打开点击EditText(性别)

发生了什么事?

我的代码:

    private Spinner sexSpinner;
    String[] arrayForSpinner = {"Male", "Female"};


    //Inside OnCreate method:
    sexSpinner = (Spinner)findViewById(R.id.spinner);
    adapter = new ArrayAdapter<String>(this, R.layout.spinner_row, arrayForSpinner);
    sexSpinner.setAdapter(adapter);


sexSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
            gender.setText(sexSpinner.getSelectedItem().toString());

        }

gender.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean hasFocus) {
            // TODO Auto-generated method stub
            if(hasFocus){
                sexSpinner.performClick();
            }
        }
    });

布局:

<EditText
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:focusable="false"
        android:longClickable="false"
        android:clickable="true"
        android:inputType="date"
        android:maxLines="1"
        android:singleLine="true"
        android:hint="Gender"
        android:textColorHint="@color/white"
        android:textColor="@color/white"
        android:ems="10"
        android:id="@+id/signup_input_gender"
        android:layout_below="@+id/signup_input_birthday"
        android:layout_centerHorizontal="true"
        android:backgroundTint="@android:color/white"/>

<Spinner
        android:background="@color/Blue"
        android:id="@+id/spinner"
        android:paddingLeft="0dp"
        android:layout_width="75dp"
        android:layout_height="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginBottom="6dp"
        android:layout_below="@+id/linearlayout"
        android:visibility="invisible" />

5 个答案:

答案 0 :(得分:2)

在此,我将展示如何使用AutoCompleteTextView。此外,由于我正在复制项目中的所有代码,因此我还添加了Imageview(删除按钮),用于重置AutoCompleteTextView

首先是XML代码(使用ConstraintLayout

<AutoCompleteTextView
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:hint="Select Gender"
        android:id="@+id/acT1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="#ffffff"
        android:textAlignment="center"            
        android:dropDownHeight="155dp"
        android:cursorVisible="false"
        android:maxLines="1"
        android:focusable="false"
        android:clickable="true"
        android:inputType="none"
        />
    <ImageView
        android:src="@drawable/clear"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        app:layout_constraintRight_toRightOf="@+id/acT1"
        app:layout_constraintBottom_toBottomOf="@+id/acT1"
        app:layout_constraintTop_toTopOf="@+id/acT1"
        android:alpha=".2"
        android:id="@+id/delButton"
        android:contentDescription="Delete Button" />

现在的Java代码:

        AutoCompleteTextView acTV1 = findViewById(R.id.acT1);
        ImageView delButton = findViewById(R.id.delButton);
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
            this, android.R.layout.simple_list_item_1, getResources()
            .getStringArray(R.array.Gender_Names));
        String selection;
        acTV1.setAdapter(arrayAdapter);
        acTV1.setCursorVisible(false);
        acTV1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                acTV1.showDropDown();              
                selection = (String) parent.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(), selection,
                Toast.LENGTH_SHORT);
                delButton.setAlpha(.8f);
            }
        });

        acTV1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View arg0) {
                acTV1.showDropDown();
            }
        });

        delButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                acTV1.setText(null);
                delButton.setAlpha(.2f);
                selection = null;                               
            }
        });

和Gender_Names数组,在strings.xml中定义:

 <string-array name="Gender_Names">
    <item>Male</item>
    <item>Female</item>  
    <item>Other</item>      
</string-array>

它的外观如下:

  1. Empty

    1. 选择了一些数据
    2. enter image description here

      除非您不使用ConstraintLayout,否则整个过程都是复制粘贴。

答案 1 :(得分:1)

要显示Spinner(使用gender.setOnClickListener()),

gender.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                flag = true;              
                sexSpinner.performClick();
            }
        });

首次显示暗示性别&#39;定义布尔全局变量&#39; flag&#39;,

    boolean flag = false;        // Here
.
.
.
sexSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (flag)        // Here
                    gender.setText(sexSpinner.getSelectedItem().toString());
            }
        });
.
.
.
gender.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                flag = true;        // Here
                sexSpinner.performClick();
            }
        });

答案 2 :(得分:0)

你可以通过这个实现你想要的目标:

在xml中添加此项而不是edittext和spinner

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, GENDERLIST);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.gender_list);
         textView.setAdapter(adapter);
textView.setOnTouchListener(new OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
            // TODO Auto-generated method stub
            textView.showDropDown();
            textView.requestFocus();
            return false;
        }
    });

,这在你的java代码中

function SetButtonStatus() {

        var Stat = document.getElementById('<%=_oTextBoxNRC.ClientID%>').value;
        var Area = document.getElementById('<%=_oTextBoxArea.ClientID%>').value;
        var Capital = document.getElementById('<%=_oTextBoxCapital.ClientID%>').value;
        var Code = document.getElementById('<%= _oTextboxBusLineCode.ClientID%>').value;
        var Gross = document.getElementById('<%=_oTextBoxGrossRec.ClientID%>').value;
        //Change these conditions as your requirement

        switch (Stat) {

            case 'N':

                if (parseFloat(Area) >= 1 && parseFloat(Capital) >= 1 && Code.length >= 1)
                    document.getElementById('<%=_oButtonAdd.ClientID%>').disabled = false;
                else
                    document.getElementById('<%=_oButtonAdd.ClientID%>').disabled = true;
                break;
            case 'R':

                if (parseFloat(Area) >= 1 && parseFloat(Gross) >= 1 && Code.length >= 1)
                    document.getElementById('<%=_oButtonAdd.ClientID%>').disabled = false;
                else
                    document.getElementById('<%=_oButtonAdd.ClientID%>').disabled = true;
                break;
        }

    }

答案 3 :(得分:0)

你点击它时没有让微调器可见,所以你看不到它

sexSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
sexSpinner.setVisibility(View.Invisible)
        gender.setText(sexSpinner.getSelectedItem().toString());

    }

gender.setOnFocusChangeListener(new View.OnFocusChangeListener() {

    @Override
    public void onFocusChange(View arg0, boolean hasFocus) {
        // TODO Auto-generated method stub
        if(hasFocus){
sexSpinner.setVisibility(View.Visible)
            sexSpinner.performClick();
        }
    }
});

答案 4 :(得分:0)

更改为对signup_input_gender使用AutoCompleteTextView,其showDropDown()将显示列表(在R.array.Gender_Names的资源中定义)

fun setupGenderWithDropdown() {
        val genderEditText = findViewById(R.id.signup_input_gender)
        val arrayAdapter = ArrayAdapter(
            this, android.R.layout.simple_list_item_1, resources.getStringArray(R.array.Gender_Names)
        )
        genderEditText.setAdapter(arrayAdapter)
        genderEditText.setOnClickListener {
            genderEditText.showDropDown()
        }
    }