android – 日期选择器对话框显示编辑文本双击

前端之家收集整理的这篇文章主要介绍了android – 日期选择器对话框显示编辑文本双击前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含两个编辑文本字段的搜索活动.我喜欢编辑文本点击日期选择器对话框来显示.但是,当我单击编辑文本时,首先显示键盘,然后在第二次单击后显示日期选择器对话框.有人能帮帮我吗?

这是活动代码

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" />

解决方法

我为xml文件中的编辑文本添加了android:focusableInTouchMode =“false”,这对我有帮助.因为第一个焦点事件被触发,然后单击事件被触发.
原文链接:https://www.f2er.com/android/314394.html

猜你在找的Android相关文章