使用Android的计算器应用程序与Web视图

时间:2013-11-27 03:10:55

标签: android android-webview calculator

我为基本计算器创建了一个Android应用程序,其中包含用于显示的Web视图。显示布局但不显示Web视图。有人可以帮我找到错误吗?

提前致谢。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >

    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent" android:layout_height="50dip"/>

    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:id="@+id/buttonLeftParen" android:text="(" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/buttonRightParen" android:text=")" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/buttonBackspace" android:layout_weight="1" android:text="B" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/buttonClear" android:text="C" android:layout_weight="1" android:layout_width="fill_parent"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:id="@+id/button7" android:text="7" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/button8" android:text="8" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/button9" android:text="9" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/buttonPlus" android:text="+" android:layout_weight="1" android:layout_width="fill_parent"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout3" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:id="@+id/button4" android:text="4" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/button5" android:text="5" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/button6" android:text="6" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/buttonMinus" android:text="-" android:layout_weight="1" android:layout_width="fill_parent"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout4" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:id="@+id/button1" android:text="1" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/button2" android:text="2" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/button3" android:text="3" android:layout_weight="1" android:layout_width="fill_parent"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/buttonTimes" android:text="*" android:layout_weight="1" android:layout_width="fill_parent"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout5" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/button0" android:text="0"></Button>
        <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/buttonDecimal" android:text="."></Button>
        <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/buttonEquals" android:text="="></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/buttonDivide" android:text="/" android:layout_width="fill_parent" android:layout_weight="1"></Button>
    </LinearLayout>

</LinearLayout>





package com.example.basiccalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends Activity {

 private WebView mWebView;
 private StringBuilder mMathString;
 private ButtonClickListener mClickListener;

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

  // Create the math string
  mMathString = new StringBuilder();

  // Enable javascript for the view
  mWebView = (WebView) findViewById(R.id.webview);
  mWebView.getSettings().setJavaScriptEnabled(true);

  // Set the listener for all the buttons
  mClickListener = new ButtonClickListener();
  int idList[] = { R.id.button0, R.id.button1, R.id.button2,
    R.id.button3, R.id.button4, R.id.button5, R.id.button6,
    R.id.button7, R.id.button8, R.id.button9, R.id.buttonLeftParen,
    R.id.buttonRightParen, R.id.buttonPlus, R.id.buttonPlus,
    R.id.buttonMinus, R.id.buttonDivide, R.id.buttonTimes,
    R.id.buttonDecimal, R.id.buttonBackspace, R.id.buttonClear };

  for(int id : idList) {
   View v = findViewById(id);
   v.setOnClickListener(mClickListener);
  }

 }

 private void updateWebView() {

  StringBuilder builder = new StringBuilder();

  builder.append("<html><body>");
  builder.append("<script type=\"text/javascript\">document.write('");
  builder.append(mMathString.toString());
  builder.append("');");
  builder.append("document.write('<br />=' + eval(\"");
  builder.append(mMathString.toString());
  builder.append("\"));</script>");
  builder.append("</body></html>");

  mWebView.loadData(builder.toString(), "application/xhtml", "UTF-8");
 }

 private class ButtonClickListener implements OnClickListener {

 @Override
  public void onClick(View v) {
   switch (v.getId()) {
   case R.id.buttonBackspace:
    if(mMathString.length() > 0)
     mMathString.deleteCharAt(mMathString.length()-1);
    break;
   case R.id.buttonClear:
    if(mMathString.length() > 0)
     mMathString.delete(0, mMathString.length());
    break;
   default:
    mMathString.append(((Button) v).getText());
   }

   updateWebView();
  }

} }

1 个答案:

答案 0 :(得分:0)

使用此行

mWebView.loadData(builder.toString(), "text/html", null)

而不是

mWebView.loadData(builder.toString(), "application/xhtml", "UTF-8")

updateWebView()函数中。