用户按下图像按钮打开 URL 而没有出现新的 window
User press image button to open a URL without a new window appearing
如何让用户按下图像按钮并按下 URL link 而无需打开新的 window?
private class HandleClick implements View.OnClickListener {
public void onClick(View arg0) {
if(arg0.getId()==R.id.imageButton){
((TextView) findViewById(R.id.textView2)).setText("Pressed: " + ++howManyClicks1);
Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (arg0.getId()==R.id.imageButton1){
((TextView) findViewById(R.id.textView3)).setText("Pressed: " + ++howManyClicks2);
Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=2");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (arg0.getId()==R.id.imageButton2){
((TextView) findViewById(R.id.textView5)).setText("Pressed: " + ++howManyClicks3);
Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (arg0.getId()==R.id.imageButton4){
((TextView) findViewById(R.id.textView6)).setText("Pressed: " + ++howManyClicks4);
Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=4");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
我希望用户按下图像按钮和 URL 而无需打开新的 window,
-澄清:
我不想向用户显示有关 he/she 按下 URL link 时发生的情况的网页,只是它已被按下。
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
这将启动一个新的 activity。如果你不想开始一个新的activity,而是希望url显示在当前activity,你需要在布局中为当前[=13]添加一个webview =] 并在 webview 中打开 url 而不是调用 startActivity.
如果您通过 Intent 调用 URL,它将打开浏览器(意味着新选项卡),而不是这个,您可以使用 Web 视图在应用程序中加载 URL
在Xml布局中:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
在Activity中:
WebView browser = (WebView) findViewById(R.id.webview);
String url = "http://www.google.com";
browser .setWebViewClient(new MyBrowser());
browser .getSettings().setLoadsImagesAutomatically(true);
browser .getSettings().setJavaScriptEnabled(true);
browser .setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser .loadUrl(url);
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
如果你不想在外部浏览器中打开 URI,你应该使用 WebView
您可以将 WebView
添加到布局中
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
然后在你的 activity:
private WebViewClient webViewClient = new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Toast.makeText(view.getContext(), error.getDescription(), Toast.LENGTH_SHORT).show();
}
};
通过id找到你的WebView
并设置地址打开
webView.setWebViewClient(webViewClient);
webView.loadUrl(yourUrl);
如何让用户按下图像按钮并按下 URL link 而无需打开新的 window?
private class HandleClick implements View.OnClickListener {
public void onClick(View arg0) {
if(arg0.getId()==R.id.imageButton){
((TextView) findViewById(R.id.textView2)).setText("Pressed: " + ++howManyClicks1);
Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (arg0.getId()==R.id.imageButton1){
((TextView) findViewById(R.id.textView3)).setText("Pressed: " + ++howManyClicks2);
Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=2");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (arg0.getId()==R.id.imageButton2){
((TextView) findViewById(R.id.textView5)).setText("Pressed: " + ++howManyClicks3);
Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (arg0.getId()==R.id.imageButton4){
((TextView) findViewById(R.id.textView6)).setText("Pressed: " + ++howManyClicks4);
Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=4");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
我希望用户按下图像按钮和 URL 而无需打开新的 window, -澄清: 我不想向用户显示有关 he/she 按下 URL link 时发生的情况的网页,只是它已被按下。
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
这将启动一个新的 activity。如果你不想开始一个新的activity,而是希望url显示在当前activity,你需要在布局中为当前[=13]添加一个webview =] 并在 webview 中打开 url 而不是调用 startActivity.
如果您通过 Intent 调用 URL,它将打开浏览器(意味着新选项卡),而不是这个,您可以使用 Web 视图在应用程序中加载 URL
在Xml布局中:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
在Activity中:
WebView browser = (WebView) findViewById(R.id.webview);
String url = "http://www.google.com";
browser .setWebViewClient(new MyBrowser());
browser .getSettings().setLoadsImagesAutomatically(true);
browser .getSettings().setJavaScriptEnabled(true);
browser .setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser .loadUrl(url);
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
如果你不想在外部浏览器中打开 URI,你应该使用 WebView
您可以将 WebView
添加到布局中
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
然后在你的 activity:
private WebViewClient webViewClient = new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Toast.makeText(view.getContext(), error.getDescription(), Toast.LENGTH_SHORT).show();
}
};
通过id找到你的WebView
并设置地址打开
webView.setWebViewClient(webViewClient);
webView.loadUrl(yourUrl);