XML.xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
app:hintTextAppearance="@style/WhiteTextInputLayout"
android:layout_marginTop="8dp"
android:id="@+id/txDept"
android:textColorHint="#FFF"
android:layout_marginBottom="8dp">
<EditText
android:id="@+id/input_department"
android:layout_width="match_parent"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:editable="false"
android:drawableRight="@drawable/ic_department"
android:inputType="text"
android:hint="Department"/>
</android.support.design.widget.TextInputLayout>
Java.file
etDepartment.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
chooseDepartment();
return true;
}
});
void chooseDepartment(){
final CharSequence[] items = {AUTO,CIVIL,CSC,EEE,ECE,EIE,ETE,IT,MBA,MCA,MECH};
final AlertDialog.Builder builder=new AlertDialog.Builder(SignUp.this);
builder.setTitle("Choose your Department");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
etDepartment.setText(items[which]);
dialog.dismiss();
}
});
builder.show();
}
当用户点击该列表对话框中显示的项目时,编辑文本将设置该文本。
但是现在它无法通过单击关闭对话框。仅两次或三次点击后,对话框将关闭。
我在logcat中看到的错误:
W / InputEventReceiver:尝试完成输入事件但输入 事件接收者已被处理
答案 0 :(得分:2)
由于您将在OnTouchListener
中执行多项操作,此对话框会多次打开,因此请尝试仅在MotionEvent.ACTION_UP
上执行操作,
试试这个,
etDepartment.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
chooseDepartment();
}
return true;
}
});