我在EditText中有一个关于setOnClickListener事件的快速问题。我有一个EditText,在触摸时会显示一个对话框。但是当我调试时,我的应用程序在第一次触摸时没有显示对话框,它只是在第二次触摸时显示。我认为问题是我使用TextInputLayout进行Edittext,但是当我尝试一些解决方案时,这些都无法正常工作。那么如何在第一次触摸时显示我的对话框?
这是我的xml文件:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="60dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_departure"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:id="@+id/actv_departure"
android:hint="Departure"
android:singleLine="true"
android:nextFocusDown="@+id/actv_arrival"
android:imeOptions="actionNext"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_arrival"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:id="@+id/actv_arrival"
android:hint="Arrival"
android:nextFocusDown="@+id/et_date"
android:imeOptions="actionNext"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_date"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_date"
android:hint="Date"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<Button android:id="@+id/btn_searching"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search"
android:background="@color/colorPrimary"
android:layout_marginTop="40dp"
android:textColor="@android:color/white"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
这是MainActivity:
public class SearchFragment extends Fragment implements View.OnClickListener{
private EditText et_date;
private TextInputLayout tilDate;
// private Button btn_searching;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
public SearchFragment(){
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
et_date = (EditText)getView().findViewById(R.id.et_date);
et_date.setInputType(InputType.TYPE_NULL);
//et_date.requestFocus();
tilDate= (TextInputLayout)getView().findViewById(R.id.input_layout_arrival);
dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
setDateTimeField();
}
private void setDateTimeField() {
try {
et_date.setOnClickListener(this);
tilDate.setOnClickListener(this);
final Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth - 1);
if (newDate.getTime().getTime() > (newCalendar.getTime().getTime())) {
final AlertDialog builder = new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage("This is a date of future! We will get current date for this review!").show();
//this code below is coppied in https://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/
final Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
builder.dismiss(); // when the task active then close the dialog
t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
}
}, 2000); // after 2 second (or 2000 miliseconds), the task will be active.
et_date.setText(dateFormatter.format(newCalendar.getTime()));
} else {
newDate.set(year, monthOfYear, dayOfMonth);
et_date.setText(dateFormatter.format(newDate.getTime()));
}
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
} catch (Exception ex) {
messages("Something Wrong!");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fargment_search, container, false);
// Inflate the layout for this fragment
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
public void messages(String msg) {
new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage(msg).setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
@Override
public void onClick(View v) {
if(v==et_date){
fromDatePickerDialog.show();
}
}
答案 0 :(得分:4)
尝试添加
android:focusable="false"
它应该有用。
当小部件获得焦点时,您的键盘会弹出。因此,要防止此行为,请将焦点设置为false。