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){」
這應該解決它。我不知道OP認爲他在用什麼語法,但這是做交換機的正確方法。 – 2014-11-23 22:04:13