微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单击edittext时是否可能显示对话框消息?

如何解决单击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 我该怎么办?     

解决方法

        用这个:
mDateDisplay.setClickable(true);
mDateDisplay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showDialog(DATE_DIALOG_ID);
    }
});
另外,您可以使用XML中的
android:onClick
属性指向Activity子类中的点击处理函数。 (当然,这几天,当几乎每个人都使用兼容性库时,您可能希望创建并显示对话框片段,而不是调用
showDialog()
,但是想法是相同的。)     ,        您可以使用OnClickListener事件来处理对话框,但是在禁用xml文件中的EditText的“ 4”之后,就像这样。 例如-
android:focusableInTouchMode=\"false\"
。 因为当第一次触摸EditText时,它将调用焦点事件,而在第二次触摸时,它将调用click事件,因此您必须先禁用焦点事件。     ,        对于我使用的同一件事:
 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).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();
    }
};

}
    ,        我遇到了类似的问题。简单来说,为了解决这个问题,我使用了以下代码:
editText.setClickable(true);
editText.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // showDialog
    }
});
在XML中:
<EditText
     ...
     android:focusableInTouchMode=\"false\" 
 />
因此,用户无法编辑EditText,只能单击它。     ,        这是完美的代码,它将为您提供所需的输出:
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,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 dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

}
    ,        我用ѭ14did通过以下方式完成此操作:
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
是否具有焦点。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。