Android webview:它不起作用

时间:2013-11-28 12:50:18

标签: android webview

我不知道我的问题在哪里 第一个xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#000"
tools:context=".Reg">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFFFFF"
    android:padding="5dp"/>

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="66dp"
    />

</LinearLayout>

第二名:java

WebView mwv;
mwv = (WebView) findViewById(R.id.webView1);
mwv.getSettings().setJavaScriptEnabled(true);
mwv.loadUrl("somepage");

该页面打开正常,但我无法点击按钮或选择文本框...我只能用键盘上的Tab进行操作。

任何想法??

2 个答案:

答案 0 :(得分:0)

请从此尝试,我认为它会对您有所帮助。

public class WebViewActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
private TextView header_maintext;
private TextView headeroptiontext;
private RelativeLayout back;
private String url_string="http://www.google.com";
private String header_maintext_string="tittle";  

/** Called when the activity is first created. */
@SuppressLint("SetJavaScriptEnabled") @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
     setContentView(R.layout.webview_layout);


     url_string = getIntent().getStringExtra("url");
     header_maintext_string = getIntent().getStringExtra("header_text");

    webview = (WebView)findViewById(R.id.webview01);
    header_maintext= (TextView)findViewById(R.id.header_maintext);
    header_maintext.setText(header_maintext_string);

    headeroptiontext = (TextView)findViewById(R.id.headeroptiontext);
    headeroptiontext.setVisibility(View.GONE);

    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(true);

      back = (RelativeLayout) findViewById(R.id.back_layout);
      back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            if(webview.canGoBack() == true)
                {
                    webview.goBack();
                }
            else
                {
                    finish();
                }
         }
    });

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    progressBar = ProgressDialog.show(WebViewActivity.this, "tittle", "Loading...");

    webview.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            Log.i(TAG, "Finished loading URL: " +url);
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            Toast.makeText(WebViewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            alertDialog.setTitle("Error");
            alertDialog.setMessage(description);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });
            alertDialog.show();
        }
    });
    webview.loadUrl(url_string);


}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(webview.canGoBack() == true){
                webview.goBack();
            }else{
                finish();
            }
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
 }
}

答案 1 :(得分:0)

 // In this example I didn't place buttons, you need to add dose to your linearlayout and then set the onclicklistener  

WebView webB;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browser); 


        webB = (WebView) findViewById(R.id.webView1);
        webB.setWebViewClient(new browserClient()); //new class so it opens links in the same Activity
        webB.loadUrl("http://www.google.com");

 }


    // the new class


    import android.webkit.WebView;
    import android.webkit.WebViewClient;


    public class browserClient extends WebViewClient {

      @Override
      public boolean shouldOverrideUrlLoading(WebView v, String url){
        v.loadUrl(url);
        return true;
      }

    }