选择后通过单选按钮打开网站

时间:2014-11-23 21:17:05

标签: android webview android-radiobutton

尝试通过单选按钮选择后打开网站,我没有取得多大成功。试过WebView,但意识到我需要使用片段,我对如何做到这一点并不是很满意。我是Java / android编程的新手,所以我怀疑我犯了很多错误。提前谢谢。

我的MainActivity.Java是: -

package apps101.dcinfo;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import android.net.Uri;



public class MainActivity extends Activity {

    RadioGroup rg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup rg = (RadioGroup) findViewById(R.id.infoDC);

        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.radioButton1:
                    public void openNews(View v) {
                        String url = "http://www.washingtonpost.com";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    }
                    break;
                case R.id.radioButton2:
                    public void openNews(View v) {
                        String url = "http://washington.org";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    };
                    break;
                case R.id.radioButton3:
                    public void openWeather(View v) {
                        String url = "http://www.weaather.com";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    };
                    Website = "http://www.weather.com";
                    break;
                case R.id.radioButton4:
                    Toast.makeText(MainActivity.this, "Ok. Have a great day!",
                            Toast.LENGTH_SHORT).show();
                    break;

                }
                };

        });

    }

    }

XML如下所示: -

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="apps101.dcinfo.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Greeting"
        android:textSize="22sp"
        android:textStyle="italic" />


    <RadioGroup
        android:id="@+id/infoDC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:textSize="30sp"
        android:textStyle="bold" >

        <RadioButton
            android:id="@+id/radioButton1"
            android:text="@string/news"
            android:onClick="openNews" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:text="@string/tourist_attractions"
            android:onClick="openTourist" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:text="@string/weather"
            android:onClick="openWeather" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:text="@string/not_now" />
    </RadioGroup>


</RelativeLayout>

我在MainActivity.java上遇到多个语法错误,但不知道如何继续.. &#34; public void openNews(查看v){&#34;

1 个答案:

答案 0 :(得分:2)

我看到的第一件事就是你需要在交换机情况下删除方法声明,比如

                case R.id.radioButton1:
                    String url = "http://www.washingtonpost.com";
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                    break;