gpt4 book ai didi

android - 如何在 webview Activity 中打开 url

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:11:28 29 4
gpt4 key购买 nike

如何在Webview Activity中打开Url

你好,

我现在想在 WebView Activity 中打开链接我的代码是扫描条形码并直接打开到浏览器的链接但是我想更改它并在 Webview 中打开我该怎么做请帮我解决这个问题

谢谢

这里是BarcodeScannerActivity的代码

   public class BarcodeScannerActivity extends AppCompatActivity {

String scanContent;
String scanFormat;
TextView textView;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode_scanner);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(BarcodeScannerActivity.this);
scanIntegrator.setPrompt("Scan");
scanIntegrator.setBeepEnabled(true);

//enable the following line if you want QR code
//scanIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);

scanIntegrator.setCaptureActivity(CaptureActivityAnyOrientation.class);
scanIntegrator.setOrientationLocked(true);
scanIntegrator.setBarcodeImageEnabled(true);
scanIntegrator.initiateScan();
}
});


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
if (scanningResult.getContents() != null) {
scanContent = scanningResult.getContents().toString();
scanFormat = scanningResult.getFormatName().toString();
}

Toast.makeText(this, scanContent + " type:" + scanFormat, Toast.LENGTH_SHORT).show();

textView.setText(scanContent + " type:" + scanFormat);



Intent browseintent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com/index.php?iduser="+ scanContent));
startActivity(browseintent);

} else {
Toast.makeText(this, "Nothing scanned", Toast.LENGTH_SHORT).show();
}
}
}

Webview Activity 代码

        public class SecondActivity extends AppCompatActivity {
Button b1;
EditText ed1;

private WebView wv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);

wv1=(WebView)findViewById(R.id.webView);
wv1.setWebViewClient(new MyBrowser());

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = ed1.getText().toString();

wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
}
});
}

private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}

最佳答案

替换下面的代码

Intent browseintent=new Intent(Intent.ACTION_VIEW, 
Uri.parse("http://www.example.com/index.php?iduser="+ scanContent));
startActivity(browseintent);

使用以下代码

Intent browseintent=new Intent(this, SecondActivity.class);
browseintent.putExtra("url","http://www.example.com/index.php?iduser="+ scanContent);
startActivity(browseintent);

这将在 intent extras 中打开带有 url 的第二个 Activity 。您可以将它设置为您的 edittext,或者您可以直接将它用于您的 webview。您可以使用以下代码在第二个 Activity 中接收 url

String url = getIntent().getExtras().getString("url");

您可以在您的按钮点击中使用它,如下所示

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = getIntent().getExtras().getString("url");

wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
}
});

关于android - 如何在 webview Activity 中打开 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49358942/

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