gpt4 book ai didi

android - 如何在 Android 中显示 WebView

转载 作者:行者123 更新时间:2023-11-29 00:21:39 27 4
gpt4 key购买 nike

我有一个二维码扫描器,然后我有一个 If else 语句,我想在 if 语句为真时显示 Webview,但 Web View 仍然显示并阻止相机 View 。我该怎么做?

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

}


}
};

最佳答案

创建两个 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"
/>

两个 Activity 类,一个显示按钮的 Activity ,另一个显示具有预定义 URL 的 WebView 的 Activity 。

文件: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>

关于android - 如何在 Android 中显示 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22610373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com