我刚刚进入Android应用程序,现在尝试使用允许用户在文本框中输入日期的界面。日期对话框应仅在聚焦文本框时显示。从对话框中选择日期后,它将显示在文本框中。现在的问题是即使文本框没有聚焦,也会显示日期对话框。有人能帮助我搞清楚我的问题吗?感谢。
TimeSheet.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText
android:layout_width="190dp"
android:layout_height="wrap_content"
android:id="@+id/txtdate"
android:layout_x="131dp"
android:layout_y="192dp"
android:hint="Touch here to select a date"/>
</AbsoluteLayout>
TimeSheet.java
package com.example.project.project;
import android.app.DatePickerDialog;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.view.View;
import android.os.Bundle;
import android.widget.Button;
import java.util.List;
import java.util.ArrayList;
import android.content.Context;
import android.content.ContentValues;
import android.widget.Toast;
import com.example.project.project.API.TimeSheetAPI;
import com.example.project.project.database.MyDatabaseHelper;
public class TimeSheet extends AppCompatActivity {
private Spinner spinner,spinner2;
private Button btnSaved;
private MyDatabaseHelper dbHelper;
private TimeSheetAPI ts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timesheet);
ts=new TimeSheetAPI(this);
}
public void onStart()
{
super.onStart();
EditText txtDate=(EditText)findViewById(R.id.txtdate);
txtDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus)
{
if(hasFocus)
{
DateDialog dialog=new DateDialog(v);
FragmentTransaction ft=getFragmentManager().beginTransaction();
dialog.show(ft,"DatePicker");
}
}
});
}
DateDialog.java
package com.example.project.project;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
/**
* Created by tongws on 9/14/2015.
*/
@SuppressLint("ValidFragment")
public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtdate;
public DateDialog(View view)
{
txtdate=(EditText)view;
}
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),this,year,month,day);
}
public void onDateSet(DatePicker view,int year, int month, int day)
{
String date=day+"-"+(month+1)+"-"+year;
txtdate.setText(date);
}
}
答案 0 :(得分:0)
活动开始后,您的EditText
一直专注。要清除EditText
上的默认焦点,请将焦点添加到父布局。这可以通过将这行代码添加到父视图中来完成。
android:focusableInTouchMode="true"
例如,
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:focusableInTouchMode="true">
<EditText
android:layout_width="190dp"
android:layout_height="wrap_content"
android:id="@+id/txtdate"
android:layout_x="131dp"
android:layout_y="192dp"
android:hint="Touch here to select a date"/>
</AbsoluteLayout>