android 开发:我想实现一个EditText点击后出现DatePickerDialog然后返回值到EditText显示?
点击后出现DatePickerDialog,不需要弹出软件键盘并且按确定后在EditText显示日期,各位大侠帮帮忙,求代码~~...
点击后出现DatePickerDialog,不需要弹出软件键盘并且按确定后在EditText显示日期,各位大侠帮帮忙,求代码~~
展开
3个回答
展开全部
邮箱吧,还没整完,整完了发给你
整完了
package com.example.editdatapickerdemo;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout line = new LinearLayout(this);
final EditText et = new EditText(this);
final Calendar cd = Calendar.getInstance();
Date date = new Date();
cd.setTime(date);
et.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, new OnDateSetListener(){
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int monthOfYear1 = monthOfYear+1;
et.setText(year+"-"+monthOfYear1+"-"+dayOfMonth);
}
},
cd.get(Calendar.YEAR),
cd.get(Calendar.MONTH),
cd.get(Calendar.DAY_OF_MONTH)).show();
}
});
et.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
new DatePickerDialog(MainActivity.this, new OnDateSetListener(){
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int monthOfYear1 = monthOfYear+1;
et.setText(year+"-"+monthOfYear1+"-"+dayOfMonth);
}
},
cd.get(Calendar.YEAR),
cd.get(Calendar.MONTH),
cd.get(Calendar.DAY_OF_MONTH)).show();
}
});
line.addView(et);
this.setContentView(line);
}
}
整完了
package com.example.editdatapickerdemo;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout line = new LinearLayout(this);
final EditText et = new EditText(this);
final Calendar cd = Calendar.getInstance();
Date date = new Date();
cd.setTime(date);
et.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, new OnDateSetListener(){
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int monthOfYear1 = monthOfYear+1;
et.setText(year+"-"+monthOfYear1+"-"+dayOfMonth);
}
},
cd.get(Calendar.YEAR),
cd.get(Calendar.MONTH),
cd.get(Calendar.DAY_OF_MONTH)).show();
}
});
et.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
new DatePickerDialog(MainActivity.this, new OnDateSetListener(){
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int monthOfYear1 = monthOfYear+1;
et.setText(year+"-"+monthOfYear1+"-"+dayOfMonth);
}
},
cd.get(Calendar.YEAR),
cd.get(Calendar.MONTH),
cd.get(Calendar.DAY_OF_MONTH)).show();
}
});
line.addView(et);
this.setContentView(line);
}
}
展开全部
嗯 可以的
你可以设置EditText的触发监听事件
我研究过,这个是在View里定义onTouchEvent()的默认处理逻辑。假如一个View是可以获得焦点的话,那么第一次点击操作就是获取焦点。这里有源代码: boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallba();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
}
}所以如果你想改变这个的话,只能自己重新实现一个onTouchEvent()而不调用super.onTouchEvent(),具体需要注意哪些东西需要你自己参考源代码,不然可能有些必须的处理过程就会漏掉了
你可以设置EditText的触发监听事件
我研究过,这个是在View里定义onTouchEvent()的默认处理逻辑。假如一个View是可以获得焦点的话,那么第一次点击操作就是获取焦点。这里有源代码: boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallba();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
}
}所以如果你想改变这个的话,只能自己重新实现一个onTouchEvent()而不调用super.onTouchEvent(),具体需要注意哪些东西需要你自己参考源代码,不然可能有些必须的处理过程就会漏掉了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为什么要用edittext呢,反正是设置日期,你可以用button,然后设置button 的onclick事件,点击button出现DatePickerDialog,最好dialog里有两个按钮,确定和取消,点击确定把所选择的值赋给button的Text
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询