显示机器人的DatePicker上点击在Javascript按钮 [英] Display android DatePicker on click of a button in Javascript

查看:147
本文介绍了显示机器人的DatePicker上点击在Javascript按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的要求: 我'装到一个的WebView 1 HTML文件。我在HTML文件中的按钮来选择日期。当我点击该按钮,我想开放的Andr​​oid日期选择器对话框。而在选择日期后,我想在显示HTML文件中选定的日期。任何人都可以指导我。请。 HTML:

 <输入类型=按钮值=选择日期的onClick =openDatePickerDialog()/>
 &其中,P ID =约会>  - < / P>
 

JavaScript的:

 函数openDatePickerDialog(){
   AndroidFunction.openDatePickerDialog();
 }

功能callFromActivity(日期){
的document.getElementById('日')的innerHTML =日期。
 }
 

我的活动:

 公共类MainActivity延伸活动{
的WebView myBrowser;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    myBrowser =(web视图)findViewById(R.id.mybrowser);

    最后MyJavaScriptInterface myJavaScriptInterface =新MyJavaScriptInterface(本);
    myBrowser.addJavascriptInterface(myJavaScriptInterfaceAndroidFunction);

    myBrowser.getSettings()setJavaScriptEnabled(真)。

    myBrowser.loadUrl(文件:///android_asset/test.html);



}

公共类MyJavaScriptInterface
{
    私人诠释mYear;
    私人诠释mMonth;
    私人诠释MDAY;
    静态最终诠释DATE_DIALOG_ID = 0;

    语境mContext;

    MyJavaScriptInterface(上下文C)
    {
        mContext = C;
    }


    公共无效openDatePickerDialog()
    {
        日历C = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        MDAY = c.get(Calendar.DAY_OF_MONTH);

        // updateDisplay();

        的ShowDialog(DATE_DIALOG_ID);
    }
    私人无效updateDisplay(){

        。字符串日期=新的StringBuilder()追加(mMonth + 1).append( - )
        .append(MDAY).append( - )
        .append(mYear).append()的ToString();

        Toast.makeText(getApplicationContext(),日期,Toast.LENGTH_LONG).show();

        myBrowser.loadUrl(JavaScript的:callFromActivity(\+日期+\));
    }


    私人DatePickerDialog.OnDateSetListener mDateSetListener =
        新DatePickerDialog.OnDateSetListener(){

        公共无效onDateSet(DatePicker的观点,年整型,
                INT monthOfYear,诠释DAYOFMONTH){
            mYear =年;
            mMonth = monthOfYear;
            MDAY = DAYOFMONTH;
            updateDisplay();
        }
    };


    受保护的对话框onCreateDialog(INT ID){
        开关(ID){
        案例DATE_DIALOG_ID:
            返回新DatePickerDialog(MainActivity.this,
                    mDateSetListener,
                    mYear,mMonth,MDAY);
        }
        返回null;
    }

}
 }
 

问题:我'没有得到DatePicker的对话框当我点击按钮。凡我'的问题呢?是我的方法正确吗?

解决方案

 公共无效openDatePickerDialog()
{
    日历C = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    MDAY = c.get(Calendar.DAY_OF_MONTH);

    // updateDisplay();
    DatePickerDialog DP =新DatePickerDialog(这一点,
            mDateSetListener,
            mYear,mMonth,MDAY);
    dp.show();
}
 

你能试试这一次机会。

Here is my requirement : I'am loading one html file on to a WebView. I have a button in html file to select the date. When i click on that button i want to open android date picker dialog. And after selecting the date, i want to display the selected date in html file. Can anyone guide me. please. HTML :

 <input type="button" value="Select Date" onClick="openDatePickerDialog()" />
 <p id = "date">--</p>

Javascript :

 function openDatePickerDialog() {
   AndroidFunction.openDatePickerDialog();
 }

function callFromActivity(date) {
document.getElementById('date').innerHTML = date;
 }

My Activity :

  public class MainActivity extends Activity {
WebView myBrowser;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myBrowser = (WebView)findViewById(R.id.mybrowser);

    final MyJavaScriptInterface myJavaScriptInterface = new MyJavaScriptInterface(this);
    myBrowser.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

    myBrowser.getSettings().setJavaScriptEnabled(true); 

    myBrowser.loadUrl("file:///android_asset/test.html");      



}

public class MyJavaScriptInterface 
{
    private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG_ID = 0;

    Context mContext;

    MyJavaScriptInterface(Context c)
    {
        mContext = c;
    }


    public void openDatePickerDialog()
    { 
        Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        //updateDisplay();

        showDialog(DATE_DIALOG_ID);
    }
    private void updateDisplay() {

        String date = new StringBuilder().append(mMonth + 1).append("-")
        .append(mDay).append("-")
        .append(mYear).append(" ").toString();

        Toast.makeText(getApplicationContext(), date, Toast.LENGTH_LONG).show();

        myBrowser.loadUrl("javascript:callFromActivity(\""+date+"\")");
    }


    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();
        }
    };


    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(MainActivity.this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
        }
        return null;
    }

}
 }

Problem : I'am not getting DatePicker Dialog When i click on button. Where i'am going wrong ? Is my approach correct ?

解决方案

public void openDatePickerDialog()
{ 
    Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    //updateDisplay();
    DatePickerDialog dp = new DatePickerDialog(this,
            mDateSetListener,
            mYear, mMonth, mDay);
    dp.show();
}

can you try this once.

这篇关于显示机器人的DatePicker上点击在Javascript按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆