我还是Android Studio的新手,并且在我单击按钮时尝试加载URL。但是,在URL中,当我键入用户名和密码时,它不会转到下一页,而是会刷新到同一页面。
我使用的代码是:
bookingBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goToWebView = new Intent(getActivity(), WebViewActivity.class);
goToWebView.putExtra("url", "https://www.airbnb.com/");
goToWebView.putExtra("activityTitle", getString(R.string.home_booking_services));
startActivity(goToWebView);
}
})
我的WebViewActivity类文件为:
public class WebViewActivity extends AppCompatActivity {
private WebView myWebView;
private LinearLayout errorLayer;
private LinearLayout pBarLayer;
private ProgressBar pBar;
private String url = "";
private String activityTitle = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sofia_web_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myWebView = (WebView) findViewById(R.id.webView); // Web view in app
pBarLayer = (LinearLayout) findViewById(R.id.progressBarLayer); // Progress bar layer
pBar = (ProgressBar) findViewById(R.id.progressBar);
errorLayer = (LinearLayout) findViewById(R.id.errorLayer);
pBar.setVisibility(ProgressBar.VISIBLE);
pBarLayer.setVisibility(ProgressBar.VISIBLE);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setJavaScriptEnabled(true); // Enable Javascript
myWebView.setWebViewClient(new WebViewClient()); // Open web link inside app
myWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100 && pBar.getVisibility() == ProgressBar.GONE) {
pBar.setVisibility(ProgressBar.VISIBLE);
pBarLayer.setVisibility(ProgressBar.VISIBLE);
}
pBar.setProgress(progress);
if (progress == 100) {
pBar.setVisibility(ProgressBar.GONE);
pBarLayer.setVisibility(ProgressBar.GONE);
}
}
});
SSLPinningWebViewClient webViewClient = new SSLPinningWebViewClient(new SSLPinningWebViewClient.LoadedListener() {
@Override
public void Loaded(final String url) {
}
@Override
public void PinningPreventedLoading(String host) {
Log.e(host, host);
}
}, WebViewActivity.this);
myWebView.setWebViewClient(webViewClient);
if (getIntent().hasExtra("url")) {
url = getIntent().getStringExtra("url");
if (getIntent().hasExtra("activityTitle"))
activityTitle = getIntent().getStringExtra("activityTitle");
}
setTitle(activityTitle);
myWebView.loadUrl(url);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
finish();
}
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
谁知道为什么加载的页面没有响应? 如果需要其他信息,请告诉我。TQ