Web görünümünde URL'leri açmak için kullanılan bir otomatik tamamlama görünümü var. Otomatik olarak ".com" ifadesini postfix olarak addetmek istiyorum. Bu, .com, .tv, .co.in, vb. Gibi uzantılara sahip değildir. Ancak sorun, girilen URL'de zaten bir uzantı olup olmadığını kontrol etmektir. . Bunu araştırdım ve bu post'u buldum, ancak yalnızca belirli bilinen uzantıları kontrol ettim. Uzatma olup olmadığını kontrol etmek istiyorum. Uzantı varsa, daha sonra url açılırsa, ".com" ifadesini url'ye postfix olarak ekler. autocompletetextview içinde url için postfix ekleyin
-1
A
cevap
0
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();
}
}
In onCreate use methos like this:
if(isURL(url)){
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}else{
url = url + ".com";
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}
Method to check if url is valid:
public boolean isURL(String url)
{
if(Patterns.WEB_URL.matcher(url).matches()){
return true;
}else{
return false;
}
}