javascript
java如何调用网页_如何使用网页开发自己的app,在网页中的按钮与自己的java代码绑定来实现打电话即javascript代码调用java代码,和java代码来调用javascript代码...
1首先是如何在自己的app里用網(wǎng)頁顯示,這樣可以較快的更新界面而不需要讓客戶端升級,方法如下:
xml文件:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/web_v"
/>
2在activity里綁定網(wǎng)頁:
public class MainActivity extends ActionBarActivity {
private WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web=(WebView) findViewById(R.id.web_v);
//相當于得到了瀏覽器
WebSettings webset=web.getSettings();
//可以設置web的屬性
webset.setJavaScriptEnabled(true);
//下面是被網(wǎng)頁里javascript調(diào)用的方法,即demo是前面是定義匿名內(nèi)的類名,里面實現(xiàn)網(wǎng)頁里javascript
//要調(diào)用的方法
web.addJavascriptInterface(new Object(){//在web控件里為javascript代碼提供使用接口
@JavascriptInterface //注意這個別掉了
public void callous()
{
System.out.println("hahahahaha");
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+"123123"));
startActivity(intent);
}
}
, "demo");
String url=new String("http://192.168.61.173:8080/myweb/zp.html");
//加載網(wǎng)頁到web控件里
web.loadUrl(url);
}
}
3、網(wǎng)頁里面的按鈕使用的超鏈接,方法如下:
聯(lián)系我們
其中demo為類名,callous就是demo類的方法,前面的window是標記,加不加沒所謂。若后面有href=“”則默認打開本網(wǎng)頁,會啟動瀏覽器,因此這里不要href=“”這個屬性。
4、如何用java代碼調(diào)用javascript的函數(shù),來顯示原來隱藏的內(nèi)容:
(1)首先給app添加一個控件:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.appdemo.MainActivity$PlaceholderFragment"
android:gravity="center_horizontal"
>
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/web_v"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt"
android:text="獲取密碼"
android:layout_weight="0"
android:onClick="getpassword"
/>
(2)然后網(wǎng)頁body前里寫入如下的代碼,即是點擊事件響應的內(nèi)容,就是把id為content內(nèi)容顯示密碼,可以放入網(wǎng)頁的任何位置
function fillContent(){
document.getElementById("content").innerHTML="隱藏密碼為:1234567890"
}
(3)在activity的點擊事件里添加如下代碼即可
public void getpassword(View v)
{
web.loadUrl("javascript:fillContent()");
}
顯示如下:1運行后,2為點擊“獲取密碼后的網(wǎng)頁:”
? ? ? ? ? ? ? ?
原文:http://www.cnblogs.com/bokeofzp/p/4758068.html
總結(jié)
以上是生活随笔為你收集整理的java如何调用网页_如何使用网页开发自己的app,在网页中的按钮与自己的java代码绑定来实现打电话即javascript代码调用java代码,和java代码来调用javascript代码...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java redis 没密码配置_如何解
- 下一篇: java mvc 获取session_S