英文:
How to setOnclickListner for the intent
问题
我想首先在myclass中加载URL(google.com)。当点击任何进一步的链接时,应在web_view.class中打开它。
我如何正确设置onclickListener来传递意图?我对Android不太了解。非常感谢您的帮助。这是我的class.java代码。
public class myclass extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
getSupportActionBar().hide();
webView = findViewById(R.id.webh);
progressBar = findViewById(R.id.progressBar);
CookieSyncManager.createInstance(this);
CookieManager.getInstance().setAcceptCookie(true);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.setScrollbarFadingEnabled(false);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
if (newProgress==100) {
progressBar.setVisibility(View.GONE);
}
}
});
this.webView.loadUrl("https://google.com");
Intent intent = new Intent(this, web_view.class);
String mUrl= "https://google.com";
intent.setData(Uri.parse(mUrl));
startActivity(intent);
}
}
英文:
I want to load the url (google.com) in myclass first. When click on any further link, it should be open in web_view.class.
How can I correctly set onclickListener to pass the intent? I'm new to android. I'll be very thankful for the help. Here is myclass.java code.
public class myclass extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
getSupportActionBar().hide();
webView = findViewById(R.id.webh);
progressBar = findViewById(R.id.progressBar);
CookieSyncManager.createInstance(this);
CookieManager.getInstance().setAcceptCookie(true);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.setScrollbarFadingEnabled(false);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
if (newProgress==100) {
progressBar.setVisibility(View.GONE);
}
}
});
this.webView.loadUrl("https://google.com");
Intent intent = new Intent(this, web_view.class);
String mUrl= "https:google.com";
intent.setData(Uri.parse(mUrl));
startActivity(intent);
}
}
答案1
得分: 1
你需要重写WebViewClient
。
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view,
WebResourceRequest request) {
Intent intent = new Intent(myclass.this, web_view.class);
intent.setData(request.getUrl());
startActivity(intent);
return true;
}
}
英文:
You will need to override WebViewClient
.
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view,
WebResourceRequest request) {
Intent intent = new Intent(myclass.this, web_view.class);
intent.setData(request.getUrl());
startActivity(intent);
return true;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论