在片段上使用微调器似乎一切都很好,但是当我添加EditText并在EditText中设置焦点时,微调器侦听器将不会响应...
我尝试了很多事情,例如更改微调器数据,删除所有破坏性代码,更改布局。
<FrameLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/button_orange"
android:layout_marginBottom="10dp" android:layout_marginTop="10dp">
<androidx.appcompat.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="match_parent" android:id="@+id/spinnerCountries"
android:spinnerMode="dropdown" android:layout_margin="5dp"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@color/white"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/input_layout_phone"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/orange">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="@string/hint_phone_number"
android:importantForAutofill="noExcludeDescendants"
android:maxLength="20"
android:drawableRight="@mipmap/icon_help_phone"
android:textColor="@color/orange"
tools:ignore="UnusedAttribute" android:background="@null" android:fitsSystemWindows="true"/>
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>
mSpinnerCountries!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, position: Int, arg3: Long) {
prefs!!["country_position"] = position
val mSelectedCountry = arg0.selectedItem as Pays?
prefs!!["user_countrycode"] = mSelectedCountry?.code
prefs!!["carrier_countryiso"] = mSelectedCountry?.iso
prefs!!["country_prefix"] = mSelectedCountry?.prefix
Timber.d("Item selected on spinner : code ${mSelectedCountry?.code}, iso ${mSelectedCountry?.iso}, prefix ${mSelectedCountry?.prefix}")
mEditText!!.text!!.clear()
mEditText!!.text!!.clearSpans()
mEditText!!.setText(mSelectedCountry?.prefix)
mEditText!!.setSelection(mSelectedCountry?.prefix!!.length)
}
override fun onNothingSelected(arg0: AdapterView<*>) {
val mSelectedCountry = arg0.selectedItem as Pays?
prefs!!["user_countrycode"] = mSelectedCountry?.code
prefs!!["carrier_countryiso"] = mSelectedCountry?.iso
prefs!!["country_prefix"] = mSelectedCountry?.prefix
Timber.d("No Item selected on spinner : code ${mSelectedCountry?.code}, iso ${mSelectedCountry?.iso}, prefix ${mSelectedCountry?.prefix}")
mEditText!!.setText(mSelectedCountry?.prefix)
mEditText!!.setSelection(mSelectedCountry?.prefix!!.length+1)
}
}
现在,即使在集中编辑文本之后,我也希望能够使用微调监听器。
答案 0 :(得分:0)
您发布的代码可以正常工作。
能否请您创建一个非常简单的项目并将此行添加到该项目中,然后检查是否一切正常?如果以下代码可用于您的简单项目,我们可以进一步调试您的主项目。如果这不起作用,则您的设备可能有问题。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@mipmap/ic_launcher"
android:layout_marginBottom="10dp" android:layout_marginTop="10dp">
<androidx.appcompat.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="match_parent" android:id="@+id/spinnerCountries"
android:spinnerMode="dropdown" android:layout_margin="5dp"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@android:color/white"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/input_layout_phone"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@android:color/holo_orange_dark">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="phone number"
android:importantForAutofill="noExcludeDescendants"
android:maxLength="20"
android:drawableRight="@mipmap/ic_launcher"
android:textColor="@android:color/holo_orange_dark"
tools:ignore="UnusedAttribute" android:background="@null" android:fitsSystemWindows="true"/>
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>
</LinearLayout>
MainActivity.kt
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, Arrays.asList("a","b","c"))
spinnerCountries.adapter =adapter
spinnerCountries!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, position: Int, arg3: Long) {
input_phone!!.text!!.clear()
input_phone!!.text!!.clearSpans()
input_phone!!.setText(adapter.getItem(position))
input_phone!!.setSelection(adapter?.getItem(position)!!.length)
}
override fun onNothingSelected(arg0: AdapterView<*>) {
}
}
}
}