我想点击一个edittext并显示对话日期,所以我试试
mDateDisplay = (EditText) findViewById(R.id.dateDisplay);
mDateDisplay.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
showDialog(DATE_DIALOG_ID);
}
}
});
对话框将显示mDateDisplay是否为按钮
但我想使用edittext
我该怎么办?
答案 0 :(得分:42)
您可以使用OnClickListener事件来处理对话框,但是在xml文件中禁用EditText的android:focusableInTouchMode
后就像这样。
例如 - android:focusableInTouchMode="false"
。
因为首次触摸EditText时会调用焦点事件,第二次触摸时会调用click事件,因此您必须先禁用焦点事件。
答案 1 :(得分:11)
使用此:
mDateDisplay.setClickable(true);
mDateDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
或者,您可以使用XML中的android:onClick
属性指向Activity子类中的单击处理函数。
(当然,当然,当几乎每个人都使用兼容性库时,你可能想要创建并显示一个对话框片段,而不是调用showDialog()
,但这个想法是一样的。)
答案 2 :(得分:5)
对于我用过的相同的事情:
sel_date.setClickable(true);
sel_date.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(DEFAULTDATESELECTOR_ID);
}
});
并将选定日期设置为EditText的值:
private DateSlider.OnDateSetListener mDateSetListener =
new DateSlider.OnDateSetListener() {
public void onDateSet(DateSlider view, Calendar selectedDate) {
// update the dateText view with the corresponding date
sel_date.setText(String.format("%te-%tB-%tY", selectedDate, selectedDate, selectedDate).trim());
}
};
我已经在Android中编辑了自定义日期选择器中的代码:http://blog.codeus.net/dateslider-1-0-an-alternative-datepicker-for-android/
您可以使用EditText替换Button,并执行与上述代码相同的操作。
当EditText
获得焦点时,键盘似乎会弹出。要防止这种情况,请将focusable设置为false:
<EditText
...
android:focusable="false"
... />
已修改
检查以下代码:
public class Main extends Activity {
EditText et1,et2;
static final int DATE_DIALOG_ID = 0;
static final int DATE_DIALOG_ID1 = 1;
private int mYear;
private int mMonth;
private int mDay;
private int mYear1;
private int mMonth1;
private int mDay1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1=(EditText)findViewById(R.id.EditText01);
et2=(EditText)findViewById(R.id.EditText02);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
et1.setText("month/year");
et2.setText("month/year");
et1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
return false;
}
});
et2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID1);
return false;
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
case DATE_DIALOG_ID1:
return new DatePickerDialog(this, mDateSetListener1, mYear1, mMonth1,
mDay1);
}
return null;
}
// updates the date in the TextView
private void updateDisplay() {
et1.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mYear));
}
private void updateDisplay1() {
et2.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth1 + 1).append("-").append(mYear1));
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year1, int monthOfYear1,
int dayOfMonth1) {
// TODO Auto-generated method stub
mYear1 = year1;
mMonth1 = monthOfYear1;
mDay1 = dayOfMonth1;
updateDisplay1();
}
};
}
答案 3 :(得分:4)
我遇到了类似的问题。简单来说,为了解决这个问题,我使用了以下代码:
var express = require('express');
var app = express();
app.all('*', function(req, res){
res.sendfile("index.html");
});
在XML中:
editText.setClickable(true);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// showDialog
}
});
因此,用户无法编辑EditText,只需单击它即可。
答案 4 :(得分:2)
这是一个完美的代码,可以为您提供所需的输出:
public class TextView_Calendar extends Activity {
EditText et1,et2;
static final int DATE_DIALOG_ID = 0;
private int mYear;
private int mMonth;
private int mDay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1=(EditText)findViewById(R.id.et1);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
et1.setText("month/year");
et1.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
showDialog(DATE_DIALOG_ID);
return false;
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
// updates the date in the TextView
private void updateDisplay() {
et1.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-").append(mMonth + 1).append("-").append(mYear));
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
}
答案 5 :(得分:1)
我用以下onFocusChangeListener
方式做到了:
mCompleteByEditText = (EditText) findViewById(R.id.completion);
mCompleteByEditText
.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(),
"timePicker");
}
}
});
hasFocus
变量控制当前EditText
是否具有焦点。