2

我正在尝试为我的学校制作一个时间表应用程序,并且我想使用一个包含所有课程的微调器。当您选择您的课程时,我希望它打开一个包含时间表的特定 URL。我尝试了很多方法,但它不起作用..我不想使用“提交”按钮..有人可以帮助我吗?

编辑:认为我想通了,但这是我的下一个问题:我希望它用 if 语句打开一个网站,但是当我尝试这样做时,它给了我一个错误:对于 CustomOnItemSelectedListener 类型,方法 findViewById(int) 是未定义的

public class CustomOnItemSelectedListener implements OnItemSelectedListener {


public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) { 
if (parent.getItemAtPosition(pos) == "h5c") {
WebView myWebView = (WebView) findViewById(R.id.webView1);
}

}


public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

}

真的希望你们理解,因为我是这方面的菜鸟:p

4

1 回答 1

1

试试这个。这是您查询的完整答案。

还可以在 AndroidManifest 中授予 Internet 权限

private WebView wv1;
String url;

Spinner spinner;
private String[] spinner_and_webview = {"Learn Spinner", "Learn WebView"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adapter0 =
            new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_item, spinner_and_webview);
    adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter0);
    spinner.setOnItemSelectedListener(onItemSelectedListener0);

    wv1 = (WebView) findViewById(R.id.webView);
    wv1.setWebViewClient(new MyBrowser());
}

AdapterView.OnItemSelectedListener onItemSelectedListener0 =
        new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {

                String sp1 = String.valueOf(spinner.getSelectedItem());
                if (sp1.contentEquals("Learn Spinner")) 
                    url = "http://developer.android.com/guide/topics/ui/controls/spinner.html";
                if (sp1.contentEquals("Learn WebView")) 
                    url = "http://developer.android.com/reference/android/webkit/WebView.html";
                wv1.getSettings().setLoadsImagesAutomatically(true);
                wv1.getSettings().setJavaScriptEnabled(true);
                wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                wv1.loadUrl(url);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        };
private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
于 2015-11-03T17:52:39.667 回答