2

这是我的要求:我正在将一个 html 文件加载到 WebView 上。我在 html 文件中有一个按钮来选择日期。当我单击该按钮时,我想打开 android 日期选择器对话框。选择日期后,我想在 html 文件中显示所选日期。谁能指导我。请。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;
 }

我的活动:

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

}
 }

问题:当我点击按钮时,我没有得到 DatePicker 对话框。我哪里错了?我的方法正确吗?

4

2 回答 2

5

这是我使用的示例代码,来自此处的代码:

在 html 代码中,添加 2 个 javascript 函数:

// 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;
}

Tag 是要完成的输入表单的 id。您可以像这样调用 javascript 函数:

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

这是实现的java代码。请注意,MyJavaScriptInterface 是在 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

就这个。希望这能有所帮助。

于 2012-10-10T15:38:49.940 回答
2
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();
}

你能试试这个吗?

于 2012-04-26T06:34:05.453 回答