我有两个带有两个不同网址的ImageButton。当我点击图像时,它会返回一个白色的屏幕。我不知道是什么问题。欢迎任何编辑或建议。
谢谢!
主要活动
public class main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ImageButton amazon = (ImageButton)findViewById(R.id.imagebutton1);
ImageButton flipkart = (ImageButton)findViewById(R.id.imagebutton2);
amazon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.amazon.com"));
WebView webview = new WebView(main.this);
setContentView(webview);
startActivity(intent);
}
});
flipkart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Flipkart",Toast.LENGTH_LONG).show();// display the toast on you tube button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.flipkart.in"));
startActivity(intent);
}
});
}
我的网络视图活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView htmlWebView = (WebView) findViewById(R.id.webView);
htmlWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSetting = htmlWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
htmlWebView.loadUrl("");
}
class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
答案 0 :(得分:0)
是否要在应用中加载网址或想要通过外部浏览器打开。我已经给出了在webview中打开url的剪切代码,如下面的应用程序所示:
WebView mynews;
ProgressBar pb;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsreadscreen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pb= (ProgressBar) findViewById(R.id.progress_bar);
mynews= (WebView) findViewById(R.id.mynews);
mynews.getSettings().setJavaScriptEnabled(true);
mynews.getSettings().setDefaultFontSize(17);
mynews.getSettings().setDisplayZoomControls(true);
mynews.getSettings().setDomStorageEnabled(true);
mynews.getSettings().setLoadsImagesAutomatically(true);
mynews.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pb.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
pb.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pb.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
});
mynews.loadUrl("https://www.google.co.in");
这是我编辑的代码
amazon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.putExtra("url","http://www.amazon.com");
startActivity(intent);
}
});
获取MainActivity.Class onCreateMethod上的值,如下所示
String url=getIntent().getStringExtra("url");
mynews.loadUrl(url);