歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android按鍵學習:RadioButton

Android按鍵學習:RadioButton

日期:2017/3/1 10:30:33   编辑:Linux編程

源程序來自網絡,做了簡單的美化和修改。

HelloActivity.java

  1. package sg131971.hello;
  2. import Android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.widget.RadioButton;
  6. import android.widget.RadioGroup;
  7. import android.widget.Toast;
  8. import android.widget.RadioGroup.OnCheckedChangeListener;
  9. public class HelloActivity extends Activity {
  10. /** Called when the activity is first created. */
  11. protected RadioGroup group;
  12. protected RadioButton radio1,radio2,radio3,radio4;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. group = (RadioGroup)findViewById(R.id.radiogroup);
  18. radio1 = (RadioButton)findViewById(R.id.button1);
  19. radio2 = (RadioButton)findViewById(R.id.button2);
  20. radio3 = (RadioButton)findViewById(R.id.button3);
  21. radio4 = (RadioButton)findViewById(R.id.button4);
  22. group.setOnCheckedChangeListener(new AnswerListener() );
  23. }
  24. class AnswerListener implements OnCheckedChangeListener {
  25. public void onCheckedChanged(RadioGroup group, int checkedId) {
  26. // TODO Auto-generated method stub
  27. if (checkedId == radio2.getId())
  28. {
  29. showMessage("正確答案:" + radio2.getText()+".恭喜你,答對了!");
  30. }
  31. else if(checkedId == radio1.getId())
  32. {
  33. showMessage("對不起!" + radio1.getText()+"雖然很多,但不是公認的最多!");
  34. }
  35. else if(checkedId == radio3.getId())
  36. {
  37. showMessage("對不起!" + radio3.getText()+"雖然很多,但不是公認的最多!");
  38. }
  39. else
  40. {
  41. showMessage("對不起!" + radio4.getText()+"雖然很多,但不是公認的最多!");
  42. }
  43. }
  44. }
  45. public void showMessage(String str)
  46. {
  47. Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
  48. toast.setGravity(Gravity.TOP, 0, 420);
  49. toast.show();
  50. }
  51. }

main.xml

[javascript]

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="@drawable/bg1"
  7. >
  8. <TextView android:text="哪個城市的美女最多?" android:textSize="30px" android:layout_height="wrap_content" android:id="@+id/mytextview" android:layout_width="fill_parent" android:textColor="#FF0000" android:layout_x="0dp" android:layout_y="80dp"></TextView>
  9. <RadioGroup android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radiogroup" android:layout_x="0dp" android:layout_y="140dp">
  10. <RadioButton android:layout_height="wrap_content" android:text="杭州" android:layout_width="wrap_content" android:id="@+id/button1"></RadioButton>
  11. <RadioButton android:layout_height="wrap_content" android:text="重慶" android:layout_width="wrap_content" android:id="@+id/button2"></RadioButton>
  12. <RadioButton android:layout_height="wrap_content" android:text="成都" android:layout_width="wrap_content" android:id="@+id/button3"></RadioButton>
  13. <RadioButton android:layout_height="wrap_content" android:text="香港" android:layout_width="wrap_content" android:id="@+id/button4"></RadioButton>
  14. </RadioGroup>
  15. </AbsoluteLayout>

運行效果圖:

Copyright © Linux教程網 All Rights Reserved