Android按键学习:Button的界面跳转(Intent)


HelloActivity.java

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

相关内容