歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android按鍵學習:Button的界面跳轉(Intent)

Android按鍵學習:Button的界面跳轉(Intent)

日期:2017/3/1 11:09:26   编辑:Linux編程

HelloActivity.java

  1. package sg131971.hello;
  2. import Android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.Toast;
  12. import android.widget.RadioGroup.OnCheckedChangeListener;
  13. public class HelloActivity extends Activity {
  14. /** Called when the activity is first created. */
  15. private RadioGroup group;
  16. private RadioButton radio1,radio2,radio3,radio4;
  17. private Button myButton;
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. group = (RadioGroup)findViewById(R.id.radiogroup);
  23. radio1 = (RadioButton)findViewById(R.id.button1);
  24. radio2 = (RadioButton)findViewById(R.id.button2);
  25. radio3 = (RadioButton)findViewById(R.id.button3);
  26. radio4 = (RadioButton)findViewById(R.id.button4);
  27. myButton = (Button)findViewById(R.id.myButton);
  28. group.setOnCheckedChangeListener(new AnswerListener());
  29. myButton.setOnClickListener(new MyButtonListener());
  30. }
  31. class AnswerListener implements OnCheckedChangeListener {
  32. public void onCheckedChanged(RadioGroup group, int checkedId) {
  33. // TODO Auto-generated method stub
  34. if (checkedId == radio2.getId())
  35. {
  36. showMessage("正確答案:" + radio2.getText()+".恭喜你,答對了!");
  37. }
  38. else if(checkedId == radio1.getId())
  39. {
  40. showMessage("對不起!" + radio1.getText()+"雖然很多,但不是公認的最多!");
  41. }
  42. else if(checkedId == radio3.getId())
  43. {
  44. showMessage("對不起!" + radio3.getText()+"雖然很多,但不是公認的最多!");
  45. }
  46. else
  47. {
  48. showMessage("對不起!" + radio4.getText()+"雖然很多,但不是公認的最多!");
  49. }
  50. }
  51. }
  52. class MyButtonListener implements OnClickListener {
  53. public void onClick(View arg0) {
  54. // TODO Auto-generated method stub
  55. Intent intent = new Intent();
  56. intent.setClass(HelloActivity.this, Second.class);
  57. HelloActivity.this.startActivity(intent);
  58. }
  59. }
  60. public void showMessage(String str)
  61. {
  62. Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
  63. toast.setGravity(Gravity.TOP, 0, 420);
  64. toast.show();
  65. }
  66. }
Main.xml
  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. <Button android:id="@+id/myButton" android:layout_height="50dp" android:layout_width="205dp" android:text="@string/submit" android:layout_x="49dp" android:layout_y="345dp"></Button>
  16. </AbsoluteLayout>
    Second.java
Copyright © Linux教程網 All Rights Reserved