如何在Android中显示WebView

时间:2014-03-24 13:10:38

标签: android webview

我有一个qr代码扫描程序然后我有一个If else语句,我想在if语句为true时显示Webview但Web视图仍然显示并阻止摄像机视图。我该怎么做?

       PreviewCallback previewCb = new PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            Camera.Parameters parameters = camera.getParameters();
            Size size = parameters.getPreviewSize();

            Image barcode = new Image(size.width, size.height, "Y800");
            barcode.setData(data);

            int result = scanner.scanImage(barcode);

            if (result != 0) {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();

                SymbolSet syms = scanner.getResults();
                WebView engine = (WebView) findViewById(R.id.web_engine);
                engine.removeAllViews();
                          for (Symbol sym : syms) {
                           String value = new String(  sym.getData());
                            if(value.startsWith("http://192.168.74.1")){
                              scanText.setText("QR Code result: " + value);
                              barcodeScanned = true;
                              engine.setVisibility(View.VISIBLE);
                              engine.loadUrl(value);
                            }
                            else {
                                scanText.setText("QR Code result 2: " + value);
                                barcodeScanned = true;
                                 engine.setVisibility(View.GONE); 
                            }
                            TextView tv = (TextView) findViewById(R.id.scanText);
                            tv.setMovementMethod(LinkMovementMethod.getInstance());
                      }

            }


        }
    };

2 个答案:

答案 0 :(得分:1)

创建两个Android布局文件 - “res / layout / main.xml”和“res / layout / webview.xml”。

文件:res / layout / main.xml

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

    <Button
        android:id="@+id/buttonUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to http://www.google.com" />

</LinearLayout>

文件:res / layout / main.xml - WebView示例

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

两个活动类,一个显示按钮的活动,另一个活动显示带有预定义URL的WebView。

文件:MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button button;

    public void onCreate(Bundle savedInstanceState) {
        final Context context = this;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonUrl);

        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            Intent intent = new Intent(context, WebViewActivity.class);
            startActivity(intent);
          }

        });

    }

}

文件:WebViewActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");

    }

}

文件:AndroidManifest.xml - 查看完整示例。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".WebViewActivity"
            android:theme="@android:style/Theme.NoTitleBar" />

        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

答案 1 :(得分:0)

试试这个......

  SymbolSet syms = scanner.getResults();
  WebView engine = (WebView) findViewById(R.id.web_engine);
            for (Symbol sym : syms) {
             String value = new String(  sym.getData());
              if(value.startsWith("http://192.168.74.1")){
                scanText.setText("QR Code result: " + value);
                barcodeScanned = true;
                engine.setVisibility(View.VISIBLE);
                engine.loadUrl(value);
              }
              else {
                  scanText.setText("QR Code result 2: " + value);
                  barcodeScanned = true;
                  engine.setVisibility(View.INVISIBLE);
              }
              TextView tv = (TextView) findViewById(R.id.scanText);
              tv.setMovementMethod(LinkMovementMethod.getInstance());
        }