如何根据按钮单击在新屏幕上的 webview 中打开 url
How to open a url in webview on new screen based on button click
您好,我已经在 Whosebug 中尝试了所有可用的解决方案,但没有任何效果,单击按钮时应用程序停止。请帮帮我。
每当我点击按钮时,应用程序就会崩溃。我希望应用程序在单击按钮时在 Web 视图中打开 url。请帮助我,非常感谢任何帮助。
任何人都可以 post 修改后的代码。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:text="@string/button01"
android:onClick="onClick"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:text="@string/button02"
android:onClick="onClick"/>
</LinearLayout>
------------------------------
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
-----------------------------------------------------
mainactivity.java
package com.example.webview;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v)
{
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
final Context context = this;
Intent intent = new Intent(context, SecondActivity.class);
startActivity(intent);
switch(v.getId())
{
case R.id.button1:
myWebView.loadUrl("http://techcrunch.com/tag/rss/");
break;
case R.id.button2:
myWebView.loadUrl("http://www.bestchance.org.au/");
break;
default:
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
--------------------
secondactivity.java
package com.example.webview;
import android.webkit.WebView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SecondActivity extends ActionBarActivity {
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
}
}
-------------------------------
您可能需要 post 您在此处编写代码以查看发生了什么,但是如果您只是想获得一个 webview 弹出窗口,您可以启动浏览器 activity 来加载该页面
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
如果你需要它成为你的activity,你需要将一个意图传递给你的新activity并取出url然后你什么时候加载它
webView.loadUrl(URL);
如果问题是崩溃 post 崩溃日志在这里,我们可以看看哪里出了问题
public void onClick(View v)
{
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
final Context context = this;
Intent intent = new Intent(context, SecondActivity.class);
String url;
switch(v.getId())
{
case R.id.button1:
url ="http://techcrunch.com/tag/rss/";
break;
case R.id.button2:
url = "http://www.bestchance.org.au/";
break;
default:
break;
}
startActivity(intent);
myWebView.loadUrl(url);
}
只需删除您的代码并将其粘贴到..您试图访问不存在的按钮,因为您启动了一个新的 activity,其中布局发生了变化并且没有调用它们的按钮..
它崩溃的原因是因为您正在调用属于另一个 activity 的 webview
。因此,只需将一个字符串从您的 activity 传递到第二个 activity,然后获取该字符串并将其加载到您的 webview
。以下是实现它的方法:
在第一个 activity 上,将您的 onClick
方法编辑为:
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
switch(v.getId())
{
case R.id.action_bar:
intent.putExtra("url", "http://techcrunch.com/tag/rss/");
startActivity(intent);
break;
case R.id.action_bar_spinner:
intent.putExtra("url", "http://www.bestchance.org.au/");
startActivity(intent);
break;
default:
break;
}
}
然后在您的 SecondActivity 中:
public class SecondActivity extends ActionBarActivity {
private WebView myWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
String url = getIntent().getStringExtra("url");
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
}
add Internet permission in your Manifest
<uses-permission android:name="android.permission.INTERNET"/>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/webview">
</WebView>
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/btn_start"
android:textSize="20sp"
android:background="@android:color/darker_gray"
android:layout_marginTop="250dp"
android:layout_gravity="center"
android:textColor="@color/white"
android:padding="30dp">
</androidx.appcompat.widget.AppCompatButton>
</LinearLayout>
MainActivity.kt
package com.example.webview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_start.setOnClickListener { web_view() }
}
私人乐趣web_view(){
webview.webViewClient = WebViewClient()
// this will load the url of the website
webview.loadUrl("your url")
// this will enable the javascript settings
webview.settings.javaScriptEnabled = true
// if you want to enable zoom feature
webview.settings.setSupportZoom(true)
}
// if you press Back button this code will work
override fun onBackPressed() {
// if your webview can go back it will go back
if (webview.canGoBack())
webview.goBack()
// if your webview cannot go back
// it will exit the application
else
super.onBackPressed()
}
}
您好,我已经在 Whosebug 中尝试了所有可用的解决方案,但没有任何效果,单击按钮时应用程序停止。请帮帮我。
每当我点击按钮时,应用程序就会崩溃。我希望应用程序在单击按钮时在 Web 视图中打开 url。请帮助我,非常感谢任何帮助。
任何人都可以 post 修改后的代码。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:text="@string/button01"
android:onClick="onClick"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:text="@string/button02"
android:onClick="onClick"/>
</LinearLayout>
------------------------------
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
-----------------------------------------------------
mainactivity.java
package com.example.webview;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v)
{
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
final Context context = this;
Intent intent = new Intent(context, SecondActivity.class);
startActivity(intent);
switch(v.getId())
{
case R.id.button1:
myWebView.loadUrl("http://techcrunch.com/tag/rss/");
break;
case R.id.button2:
myWebView.loadUrl("http://www.bestchance.org.au/");
break;
default:
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
--------------------
secondactivity.java
package com.example.webview;
import android.webkit.WebView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SecondActivity extends ActionBarActivity {
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
}
}
-------------------------------
您可能需要 post 您在此处编写代码以查看发生了什么,但是如果您只是想获得一个 webview 弹出窗口,您可以启动浏览器 activity 来加载该页面
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
如果你需要它成为你的activity,你需要将一个意图传递给你的新activity并取出url然后你什么时候加载它
webView.loadUrl(URL);
如果问题是崩溃 post 崩溃日志在这里,我们可以看看哪里出了问题
public void onClick(View v)
{
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
final Context context = this;
Intent intent = new Intent(context, SecondActivity.class);
String url;
switch(v.getId())
{
case R.id.button1:
url ="http://techcrunch.com/tag/rss/";
break;
case R.id.button2:
url = "http://www.bestchance.org.au/";
break;
default:
break;
}
startActivity(intent);
myWebView.loadUrl(url);
}
只需删除您的代码并将其粘贴到..您试图访问不存在的按钮,因为您启动了一个新的 activity,其中布局发生了变化并且没有调用它们的按钮..
它崩溃的原因是因为您正在调用属于另一个 activity 的 webview
。因此,只需将一个字符串从您的 activity 传递到第二个 activity,然后获取该字符串并将其加载到您的 webview
。以下是实现它的方法:
在第一个 activity 上,将您的 onClick
方法编辑为:
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
switch(v.getId())
{
case R.id.action_bar:
intent.putExtra("url", "http://techcrunch.com/tag/rss/");
startActivity(intent);
break;
case R.id.action_bar_spinner:
intent.putExtra("url", "http://www.bestchance.org.au/");
startActivity(intent);
break;
default:
break;
}
}
然后在您的 SecondActivity 中:
public class SecondActivity extends ActionBarActivity {
private WebView myWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
String url = getIntent().getStringExtra("url");
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
}
add Internet permission in your Manifest
<uses-permission android:name="android.permission.INTERNET"/>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/webview">
</WebView>
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/btn_start"
android:textSize="20sp"
android:background="@android:color/darker_gray"
android:layout_marginTop="250dp"
android:layout_gravity="center"
android:textColor="@color/white"
android:padding="30dp">
</androidx.appcompat.widget.AppCompatButton>
</LinearLayout>
MainActivity.kt
package com.example.webview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_start.setOnClickListener { web_view() }
}
私人乐趣web_view(){ webview.webViewClient = WebViewClient()
// this will load the url of the website
webview.loadUrl("your url")
// this will enable the javascript settings
webview.settings.javaScriptEnabled = true
// if you want to enable zoom feature
webview.settings.setSupportZoom(true)
}
// if you press Back button this code will work
override fun onBackPressed() {
// if your webview can go back it will go back
if (webview.canGoBack())
webview.goBack()
// if your webview cannot go back
// it will exit the application
else
super.onBackPressed()
}
}