gpt4 book ai didi

android - 通过单击 EditText 打开微调器

转载 作者:搜寻专家 更新时间:2023-11-01 08:23:33 24 4
gpt4 key购买 nike

很抱歉打扰你们,但我一直在努力让它发挥作用。

我想让它做什么:

EditText 以显示提示。

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


它实际上在做什么:“男性”我的微调器中的第一个元素在用户点击它之前就已经放入我的 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" />

最佳答案

在这里,我将展示如何为此使用 AutoCompleteTextView。此外,当我从项目中复制所有代码时,我还添加了用于重置 AutoCompleteTextViewImageview(删除按钮)。

首先是 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>

它是这样的:

Empty

  1. 选择了一些数据

enter image description here

除非您不使用 ConstraintLayout,否则整件事就是复制粘贴。

关于android - 通过单击 EditText 打开微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47666321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com