Display android DatePicker on click of a button in Javascript

前端 未结 2 905
清酒与你
清酒与你 2021-01-03 12:38

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 da

相关标签:
2条回答
  • 2021-01-03 13:09

    Here is a sample code I use do show, derived from the code here:

    In the html code, add 2 javascript functions:

    // Fonction d'appel calendrier Android
    function f_CallCalendar(Tag)
    {
        MSSAndroidFunction.openDatePickerDialog(Tag);
    }
    // Fonction de retour de la date
    function callFromActivity_RetDate(Tag, data) {
        document.Form.vDate.value = data;
    }
    

    The Tag is the id of the input form to be completed. You call the javascript functions like this:

    <input name="vDate" type="text" size="11" />&nbsp;
    <input name="Submit" type="button" onclick="f_CallCalendar('vDate')" value="Calendrier*" />
    

    And here is the java code implemented. Note that the MyJavaScriptInterface is declared inside the MainActivity:

    public class MainActivity extends Activity 
            implements TextWatcher{
        WebView MainWebView;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            MainWebView = (WebView)findViewById(R.id.main_webview);
            MainWebView.getSettings().setJavaScriptEnabled(true);
            final MyJavaScriptInterface myJavaScriptInterface = new MyJavaScriptInterface(this);
            MainWebView.addJavascriptInterface(myJavaScriptInterface, "MyJavaScriptInterface");
        }
    
        // Classe de prise en charge du java privé
        public class MyJavaScriptInterface
        {
            public String m_szTagId;
    
            Context mContext;
    
            MyJavaScriptInterface(Context c)
            {
                mContext = c;
            }
    
            public void openDatePickerDialog(String szTagId)
            {
                m_szTagId = szTagId;
                Calendar c = Calendar.getInstance();
                DatePickerDialog dp = new DatePickerDialog(mContext, new OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        String szDate = String.format("%02d/%02d/%04d", dayOfMonth, monthOfYear+1, year);
                        MainWebView.loadUrl("javascript:callFromActivity_RetDate(\""+m_szTagId+"\", \""+szDate+"\")");
                    } }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
                dp.show();
            }
    
        } // Class MyJavaScriptInterface
    
    } // class MainActivity
    

    Here is it. Hope this can help.

    0 讨论(0)
  • 2021-01-03 13:29
    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.

    0 讨论(0)
提交回复
热议问题