標籤:
1首先是如何在自己的app裡用網頁顯示,這樣可以較快的更新介面而不需要讓用戶端升級,方法如下:
xml檔案:
<WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/web_v" />
2在activity裡綁定網頁:
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); //下面是被網頁裡javascript調用的方法,即demo是前面是定義匿名內的類名,裡面實現網頁裡javascript //要調用的方法 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"); //載入網頁到web控制項裡web.loadUrl(url); } }
3、網頁裡面的按鈕使用的超連結,方法如下:
<p><a onClick="window.demo.callous()">聯絡我們</a></p>
其中demo為類名,callous就是demo類的方法,前面的window是標記,加不加沒所謂。若後面有href=“”則預設開啟本網頁,會啟動瀏覽器,因此這裡不要href=“”這個屬性。
4、如何用java代碼調用javascript的函數,來顯示原來隱藏的內容:
(1)首先給app添加一個控制項:
<LinearLayout 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" > <WebView android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/web_v" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt" android:text="擷取密碼" android:layout_weight="0" android:onClick="getpassword" /> </LinearLayout>
(2)然後網頁body前裡寫入如下的代碼,即是點擊事件響應的內容,就是把id為content內容顯示密碼,<content>可以放入網頁的任何位置
<script>function fillContent(){document.getElementById("content").innerHTML="隱藏密碼為:1234567890"}</script><body>
<p id="content"></p>
(3)在activity的點擊事件裡添加如下代碼即可
public void getpassword(View v) { web.loadUrl("javascript:fillContent()"); }
顯示如下:1運行後,2為點擊“擷取密碼後的網頁:”
如何使用網頁開發自己的app,在網頁中的按鈕與自己的java代碼綁定來實現打電話即javascript代碼調用java代碼,和java代碼來調用javascript代碼