在Web视图中加载页面时处理android中的键

时间:2013-01-19 13:31:44

标签: android android-webview

我在按键时遇到这个重写方法的问题。问题是我收到了这个错误?我不知道我需要把这个方法放在我的活动中。

这是我的全部代码:

public class UgWeb extends Activity {
    ListView list_ugweb;
    WebView web;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ugweb);
        list_ugweb = (ListView)findViewById(R.id.list_ugweb);
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) 
        {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
            {
                web.goBack();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }

        String[] ugweb = new String[] {"Studentsite", "Staffsite", "BAAK", "Seminar", "Workshop", "Virtual Class", "iLab", "Gunadarma"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, ugweb)
                {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent)
                    {
                        View view = super.getView(position, convertView, parent);
                        TextView textview = (TextView)view.findViewById(android.R.id.text1);
                        textview.setTextColor(Color.WHITE);
                        return view;
                    }
                };
        list_ugweb.setAdapter(adapter);
        list_ugweb.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
            {
                String url;
                switch(pos)
                {
                    case 0 : url = "http://www.studentsite.gunadarma.ac.id"; 
                             webview(url);
                             break;
                    case 1 : url = "http://www.staffsite.gunadarma.ac.id";
                             webview(url);
                             break;
                    case 2 : url = "http://www.baak.gunadarma.ac.id";
                             webview(url);
                             break;
                    case 3 : url = "http://www.seminar.gunadarma.ac.id";
                             webview(url);
                             break;
                    case 4 : url = "http://www.lepkom.gunadarma.ac.id";
                             webview(url);
                             break;
                    case 5 : url = "http://www.v-class.gunadarma.ac.id";
                             webview(url);
                             break;
                    case 6 : url = "http://www.ilab.gunadarma.ac.id";
                             webview(url);
                             break;
                    case 7 : url = "http://www.gunadarma.ac.id";
                             webview(url);
                             break;
                    default : break;
                }
            }
        });
    }

    public void webview(String loadurl)
    {
        final String url = loadurl;
        setContentView(R.layout.webview);
        web = (WebView)findViewById(R.id.view);
        final Activity activity = UgWeb.this;
        final ProgressDialog progressdialog = new ProgressDialog(activity);
        progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressdialog.setCancelable(true);
        web.getSettings().setBuiltInZoomControls(true);
        web.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view, int progress)
            {
                progressdialog.show();
                progressdialog.setMessage("Please wait..");
                progressdialog.setProgress(0);
                activity.setProgress(progress * 1000);
                if(progress == 100 && progressdialog.isShowing())
                {
                    progressdialog.dismiss();
                }
            }
        });
        web.setWebViewClient(new WebViewClient());
        web.loadUrl(url);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_ugsimpliy_beta, menu);
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

在onCreate()后使用此方法:

public class UgWeb extends Activity {

            @Override
            public void onCreate(Bundle savedInstanceState) {
                .......
            }

            @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) 
            {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
                {
                    web.goBack();
                    return true;
                }
                return super.onKeyDown(keyCode, event);
            }
}