我有一个包含两个编辑文本字段的搜索活动。我喜欢编辑文本点击日期选择器对话框来显示。但是,当我单击编辑文本时,首先显示键盘,然后在第二次单击后显示日期选择器对话框。有人能帮助我吗?
这是活动代码
package com.example.firstdemoapp.activities;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import com.example.firstdemoapp.R;
import com.example.firstdemoapp.model.StatusDK;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
public class SearchingTaxActivity extends Activity implements OnClickListener,
DatePickerDialog.OnDateSetListener, OnItemSelectedListener {
private Calendar calendarFrom;
private Calendar calendarTo;
private String myFormat;
private SimpleDateFormat sdf;
private EditText dateFrom;
private EditText dateTo;
private EditText activeEditText;
private Calendar activeCalendar;
private Spinner spinnerStatusDK;
private ArrayAdapter spinnerArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_tax);
calendarFrom = Calendar.getInstance();
calendarTo = Calendar.getInstance();
myFormat="dd/MM/yyyy";
sdf = new SimpleDateFormat(myFormat, Locale.US);
dateFrom = (EditText) findViewById(R.id.dateFrom);
dateTo = (EditText) findViewById(R.id.dateTo);
spinnerStatusDK=(Spinner)findViewById(R.id.spinnerStatusDK);
spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, new StatusDK[] {
new StatusDK( 0, "0" ),
new StatusDK( 1, "1" ),
new StatusDK( 2, "2" ),
});
spinnerStatusDK.setAdapter(spinnerArrayAdapter);
spinnerStatusDK.setOnItemSelectedListener(this);
dateFrom.setOnClickListener(this);
dateTo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == dateFrom) {
activeCalendar = calendarFrom;
activeEditText = dateFrom;
} else if (v == dateTo) {
activeCalendar = calendarTo;
activeEditText = dateTo;
}
new DatePickerDialog(SearchingTaxActivity.this, this,
activeCalendar.get(Calendar.YEAR),
activeCalendar.get(Calendar.MONTH),
activeCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
activeCalendar.set(Calendar.YEAR, year);
activeCalendar.set(Calendar.MONTH, monthOfYear);
activeCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if (activeEditText != null) {
activeEditText.setText(sdf.format(activeCalendar.getTime()));
}
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
并且活动的布局是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dateFromTextView"
/>
<EditText
android:id="@+id/dateFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/edit_datefrom"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dateToTextView"
/>
<EditText
android:id="@+id/dateTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/edit_dateto"
/>
<Spinner
android:id="@+id/spinnerStatusDK"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
答案 0 :(得分:35)
我添加了android:focusableInTouchMode =&#34; false&#34;用于编辑xml文件中的文本,这对我有帮助。因为第一个焦点事件被触发,然后点击事件被触发。
答案 1 :(得分:0)
您可以尝试使用InputMethodManager类
隐藏键盘private void hideKeyboard(EditText et) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
答案 2 :(得分:0)
这是最好的解决方案,对于打开任何DatePicker
&amp;来自TimePicker
的{{1}}对话,
EditText