2014-11-23 63 views
0

尝试通过单选按钮选择后打开网站,我没有太大的成功。尝试了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> 

我在下面lines..not肯定混得MainActivity.java多个语法错误如何继续.. “public void openNews(View v){”

回答

2

我看到的第一件事是你需要删除开关案例中的方法decleration,如

   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; 
+0

这应该解决它。我不知道OP认为他在用什么语法,但这是做交换机的正确方法。 – 2014-11-23 22:04:13