本文介绍了添加变量的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的变量添加到网页视图。举例来说,我不希望是:http:// example.com,但是,我想MYVARIABLE。
我的code是:

 公共类经理扩展活动{
EDITTEXT的EditText; @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.manager);
        的WebView =(的WebView)findViewById(R.id.webview);
     // Instanciation杜的WebView ...
        的WebView wvSite =(的WebView)findViewById(R.id.webview);        //...on活跃的JavaScript ...
        WebSettings webSettings = wvSite.getSettings();
        webSettings.setJavaScriptEnabled(真);        //...et电荷拉页
        wvSite.loadUrl(http://example.com);
        loadSaved preferences();
 }
 私人无效loadSaved preferences(){
        共享preferences共享preferences = preferenceManager
                .getDefaultShared preferences(本);        字符串名称=共享preferences.getString(storedName,提供yourname);        editText.setText(名);
    }}

和我想通过变量名来替换mysite.com。
可能吗?而如何?

编辑:

 公共类经理扩展活动{
EDITTEXT的EditText;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.manager);
    EDITTEXT =(EditText上)findViewById(R.id.edittext); // Instanciation杜的WebView ...
    的WebView wvSite =(的WebView)findViewById(R.id.webview);    //...on活跃的JavaScript ...
    WebSettings webSettings = wvSite.getSettings();
    webSettings.setJavaScriptEnabled(真);    //...et电荷拉页
    wvSite.loadUrl(editText.getText()的toString());
    loadSaved preferences();
 }
 私人无效loadSaved preferences(){
    共享preferences共享preferences = preferenceManager
            .getDefaultShared preferences(本);    字符串名称=共享preferences.getString(storedName,提供yourname);    editText.setText(名);
    }}


解决方案

是的。首页发现您的EditText(中的onCreate),那么你可以使用它。结果
这样的:

  EDITTEXT =(EditText上)findViewById(R.id.edittext);

或者,你会得到一个NullPointerException异常。

然后地方(在按钮的点击监听器,例如):

  wvSite.loadUrl(editText.getText()的toString());


在换句话说:

@覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.manager);

  //添加此行
    EDITTEXT =(EditText上)findViewById(R.id.edittext);    的WebView =(的WebView)findViewById(R.id.webview);
    // ...    //跳过此行
    //wvSite.loadUrl(\"http://example.com);

当你wanto加载页面:

 字符串someURL =htt​​p://www.google.com
    wvSite.loadUrl(someURL);

记住必须有一些有效的URL在你的EditText ,为了您的WebView加载任何

I want to add my variable to the webview. For example, I don't want : http:// example.com but, I want myvariable. My code is :

public class manager extends Activity{
EditText editText;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manager);
        WebView = (WebView) findViewById(R.id.webview);
     // Instanciation du WebView...
        WebView wvSite = (WebView)findViewById(R.id.webview);

        //...on active JavaScript...
        WebSettings webSettings = wvSite.getSettings();
        webSettings.setJavaScriptEnabled(true);

        //...et on charge la page
        wvSite.loadUrl("http://example.com");
        loadSavedPreferences();
 }
 private void loadSavedPreferences() {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);

        String name = sharedPreferences.getString("storedName", "YourName");

        editText.setText(name);
    }

}

And I want to replace mysite.com by the variable name. Is it possible? and How?

Edit :

public class manager extends Activity{
EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.manager);
    editText = (EditText) findViewById(R.id.edittext);

 // Instanciation du WebView...
    WebView wvSite = (WebView)findViewById(R.id.webview);

    //...on active JavaScript...
    WebSettings webSettings = wvSite.getSettings();
    webSettings.setJavaScriptEnabled(true);

    //...et on charge la page
    wvSite.loadUrl(editText.getText().toString());
    loadSavedPreferences();
 }
 private void loadSavedPreferences() {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);

    String name = sharedPreferences.getString("storedName", "YourName");

    editText.setText(name);
    }

}
解决方案

Yes. First find your EditText (in onCreate), then you can use it.
Like:

editText = (EditText) findViewById(R.id.edittext);

Or you will get a NullPointerException.

Then somewhere (in the click listener of a button, for instance):

wvSite.loadUrl(editText.getText().toString());


In other words:

@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.manager);

    // Add this line
    editText = (EditText) findViewById(R.id.edittext);

    WebView = (WebView) findViewById(R.id.webview);
    // ...

    // skip this line
    //wvSite.loadUrl("http://example.com");

When you wanto to load the page:

    String someURL = "http://www.google.com";
    wvSite.loadUrl(someURL);

Mind that there must be some valid URL in your EditText, in order for your WebView to load anything

这篇关于添加变量的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 00:17