我有一个包含两个编辑文本字段的搜索活动.我喜欢编辑文本点击日期选择器对话框来显示.但是,当我单击编辑文本时,首先显示键盘,然后在第二次单击后显示日期选择器对话框.有人能帮帮我吗?
这是活动代码
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" />