-1
私はwebviewでURLを開くために使用されるautocompletetextviewを持っています。 .com、.tv、.co.inなどの拡張子を持たないURLの後ろに ".com"を自動的に追加したいのですが、入力されたURLにすでに拡張子があるかどうかを確認する方法があります。私はそれを探して、これがpostであることを発見しましたが、それは特定の既知の拡張子だけをチェックしました。拡張があるかどうかをチェックしたいもし拡張子が存在しない場合はurlを開き、 ".com"をurlの後置子として追加します。autocompletetextviewのurlに後置を追加します
public class MainActivity extends AppCompatActivity implements AdvancedWebView.Listener {
private AdvancedWebView mWebView;
private AutoCompleteTextView urlEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlEditText = (AutoCompleteTextView) findViewById(R.id.urlField);
mWebView = (AdvancedWebView) findViewById(R.id.webView);
mWebView.setListener(this, this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setGeolocationEnabled(false);
mWebView.setMixedContentAllowed(true);
mWebView.setCookiesEnabled(true);
mWebView.setThirdPartyCookiesEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView v, String url){
v.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
//Toast.makeText(MainActivity.this, "Finished loading", Toast.LENGTH_SHORT).show();
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT).show();
}
});
mWebView.addHttpHeader("X-Requested-With", "");
Button openUrl = (Button) findViewById(R.id.goButton);
openUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
String url = urlEditText.getText().toString();
if(!url.contains("http://www."))
url = "http://www." + url;
// Here should be the code to check for extensions
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}
});
}
@SuppressLint("NewApi")
@Override
protected void onResume() {
super.onResume();
mWebView.onResume();
// ...
}
@SuppressLint("NewApi")
@Override
protected void onPause() {
mWebView.onPause();
// ...
super.onPause();
}
@Override
protected void onDestroy() {
mWebView.onDestroy();
// ...
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mWebView.onActivityResult(requestCode, resultCode, intent);
// ...
}
@Override
public void onBackPressed() {
if (!mWebView.onBackPressed()) { return; }
// ...
super.onBackPressed();
}
@Override
public void onPageStarted(String url, Bitmap favicon) {
mWebView.setVisibility(View.INVISIBLE);
}
@Override
public void onPageFinished(String url) {
mWebView.setVisibility(View.VISIBLE);
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "onPageError(errorCode = "+errorCode+", description = "+description+", failingUrl = "+failingUrl+")", Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadRequested(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Toast.makeText(MainActivity.this, "onDownloadRequested(url = "+url+", userAgent = "+userAgent+", contentDisposition = "+contentDisposition+", mimetype = "+mimetype+", contentLength = "+contentLength+")", Toast.LENGTH_LONG).show();
\t \t /*final String filename = UUID.randomUUID().toString();
\t \t if (AdvancedWebView.handleDownload(this, url, filename)) {
\t \t \t // download successfully handled
\t \t }
\t \t else {
\t \t \t // download couldn't be handled because user has disabled download manager app on the device
\t \t }*/
}
@Override
public void onExternalPageRequest(String url) {
Toast.makeText(MainActivity.this, "onExternalPageRequest(url = "+url+")", Toast.LENGTH_SHORT).show();
}
}