Android使用Webview中的按钮打开EditText链接

时间:2016-03-15 19:39:48

标签: java android

我在另一篇文章中发现了这段代码,我想知道如何在webView中打开这个url而不是默认的Android浏览器。

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LinkActivity extends Activity {

EditText txtLink;
Button btnOpenLink;
String defaultLink;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_link);

    defaultLink = "http://www.google.com";

    txtLink = (EditText) findViewById(R.id.editTextLink);
    btnOpenLink = (Button) findViewById(R.id.buttonOpenLink);
    btnOpenLink.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String page = txtLink.getText().toString();
            if(!TextUtils.isEmpty(page)){
                Uri uri = Uri.parse(defaultLink+"/"+page);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }else{
                Toast.makeText(LinkActivity.this, "Please enter page on editText!!", Toast.LENGTH_LONG).show();
            }
        }
    });
}

}

我希望这个网址在应用中打开,而不是打开浏览器转到该链接。任何帮助肯定会受到赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

activity_link中,添加如下的WebView对象

<WebView
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

,您的LinkActivity应如下所示:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LinkActivity extends Activity {

EditText txtLink;
Button btnOpenLink;
String defaultLink;
WebView mWebView;
String mUrl = "";
SharedPreferences mPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_link);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    defaultLink = "http://www.google.com";

    mWebView = (WebView) findViewById(R.id.web_view);
    txtLink = (EditText) findViewById(R.id.editTextLink);
    btnOpenLink = (Button) findViewById(R.id.buttonOpenLink);
    btnOpenLink.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String page = txtLink.getText().toString();
            if(!TextUtils.isEmpty(page)){
                mUrl = defaultLink+"/"+page;
                mWebView.loadUrl(url);
            }else{
                Toast.makeText(LinkActivity.this, "Please enter page on editText!!", Toast.LENGTH_LONG).show();
            }
        }
    });
}

// This method will reloads the last opened page in the web view..!
@Override
protected void onResume()
{
    super.onResume();
    String url = mPrefs.getString("url", "");

    if (!url.equalsignorecase(""))
    {
        mUrl = url;
        txtLink.setText(url);
        mWebView.loadUrl(url);
    }
}

// And this will save the last loaded link in the Shared Preferences(Local Storage)
@Override
protected void onPause()
{
    super.onPause();
    mPrefs.edit().putString("url", mUrl).commit();
}
}

有关webview的更多信息,请参阅以下网址: http://developer.android.com/reference/android/webkit/WebView.html

要了解有关共享偏好设置的更多信息,请访问以下网址:

http://developer.android.com/reference/android/content/SharedPreferences.html