首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >你能帮我写这段代码吗?

你能帮我写这段代码吗?
EN

Stack Overflow用户
提问于 2014-03-09 00:08:17
回答 2查看 124关注 0票数 0

问题:当我输入一个ID并按下按钮时,它不会加载ID的网页...

"a url“是我试图创建的网站页面,(隐藏的原因有很多)

它应该是工作的,它加载第一个站点,但当我尝试调用I时,WebView没有改变……谁能帮帮我?-Thanks,JG1

我的代码:

代码语言:javascript
运行
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wv.setWebViewClient(new OurViewClient());
    wv.getSettings().setJavaScriptEnabled(true);
    try {
        String url = "a url";
        wv.loadUrl(url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    String lid = "0";

    //Clicking button changes to the color

}

final EditText idbox = (EditText) findViewById(R.id.editText1);
final Button idbutton = (Button) findViewById(R.id.idbtn);
final WebView wv = (WebView) findViewById(R.id.webView1);

public void onClick(View v) {
    idbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            String lid = idbox.getText().toString();
            if (lid == "1") {
                wv.setWebViewClient(new OurViewClient());
                try {
                    String urla = "a url";
                    wv.loadUrl(urla);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (lid == "2") {
                wv.setWebViewClient(new OurViewClient());
                try {
                    String urlb = "a url";
                    wv.loadUrl(urlb);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (lid == "3") {
                wv.setWebViewClient(new OurViewClient());
                try {
                    String urlc = "a url";
                    wv.loadUrl(urlc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
EN

回答 2

Stack Overflow用户

发布于 2014-03-09 00:59:08

我猜,您已经被称为相同的URL值,如(“url")。如果是,请尝试在web视图中加载不同的URL。

如果没有,请在代码中进行以下更改,

  • 在获得EditText值的地方放置断点来调试代码。

String lid = idbox.getText().toString();//检查lid is null or not

  • 更改if条件如下,

if(lid.equalsIgnoreCase("1")) {//用于呈现网页的任务}

  • 检查您的自定义视图客户端类。
票数 0
EN

Stack Overflow用户

发布于 2014-03-09 02:35:33

不要紧,你写的onClick()方法定义是错误的!

我迷惑是因为,

你是否已经在中添加了

  • 的onclick(android:onClick="onClick")函数。

尽管我对您的代码做了一些修改,但

activity_main.xml:

代码语言:javascript
运行
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2" >

    <EditText
        android:id="@+id/urlValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:layout_weight="1.5" />

    <Button
        android:id="@+id/urlBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:layout_weight="0.5" 
        android:text="load"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

MainActivity.java:

代码语言:javascript
运行
复制
public class MainActivity extends Activity {

private EditText getUrlValue;
private Button loadUrl;
private WebView webView;
String loadId = "";
String URL_ONE = "a_url";
String URL_TWO = "b_url";
String URL_THREE = "c_url";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    getUrlValue = (EditText)findViewById(R.id.urlValue);
    loadUrl = (Button)findViewById(R.id.urlBtn);
    webView = (WebView)findViewById(R.id.webView);
    
    //initial view for webView
    getUrlValue.setText("1"); //here web page will load first url= "a url"
    
    webView.setWebViewClient(new OurViewClient());
    
    //onClick Event for load url button
    loadUrl.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
        
            loadId = getUrlValue.getText().toString();
            if(loadId.equalsIgnoreCase("1")){
                try{
                    
                    webView.loadUrl(URL_ONE);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
            }
            else if(loadId.equalsIgnoreCase("2")){
                try{
                    webView.loadUrl(URL_TWO);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                
            }
            else{
                try{
                    webView.loadUrl(URL_THREE);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
           }
        }
       });
        
    }
 }

  • 记得在清单xml文件中添加internet权限。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22271693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档