如何为意图设置点击监听器

huangapple go评论77阅读模式
英文:

How to setOnclickListner for the intent

问题

我想首先在myclass中加载URLgoogle.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;
    }
}

huangapple
  • 本文由 发表于 2020年10月14日 23:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64356144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定