Android按键学习:Button的界面跳转(Intent)
Android按键学习:Button的界面跳转(Intent)
HelloActivity.java
- package sg131971.hello;
- import Android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- public class HelloActivity extends Activity {
- /** Called when the activity is first created. */
- private RadioGroup group;
- private RadioButton radio1,radio2,radio3,radio4;
- private Button myButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- group = (RadioGroup)findViewById(R.id.radiogroup);
- radio1 = (RadioButton)findViewById(R.id.button1);
- radio2 = (RadioButton)findViewById(R.id.button2);
- radio3 = (RadioButton)findViewById(R.id.button3);
- radio4 = (RadioButton)findViewById(R.id.button4);
- myButton = (Button)findViewById(R.id.myButton);
- group.setOnCheckedChangeListener(new AnswerListener());
- myButton.setOnClickListener(new MyButtonListener());
- }
- class AnswerListener implements OnCheckedChangeListener {
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if (checkedId == radio2.getId())
- {
- showMessage("正确答案:" + radio2.getText()+".恭喜你,答对了!");
- }
- else if(checkedId == radio1.getId())
- {
- showMessage("对不起!" + radio1.getText()+"虽然很多,但不是公认的最多!");
- }
- else if(checkedId == radio3.getId())
- {
- showMessage("对不起!" + radio3.getText()+"虽然很多,但不是公认的最多!");
- }
- else
- {
- showMessage("对不起!" + radio4.getText()+"虽然很多,但不是公认的最多!");
- }
- }
- }
- class MyButtonListener implements OnClickListener {
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- intent.setClass(HelloActivity.this, Second.class);
- HelloActivity.this.startActivity(intent);
- }
- }
- public void showMessage(String str)
- {
- Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.TOP, 0, 420);
- toast.show();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/bg1"
- >
- <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>
- <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">
- <RadioButton android:layout_height="wrap_content" android:text="杭州" android:layout_width="wrap_content" android:id="@+id/button1"></RadioButton>
- <RadioButton android:layout_height="wrap_content" android:text="重庆" android:layout_width="wrap_content" android:id="@+id/button2"></RadioButton>
- <RadioButton android:layout_height="wrap_content" android:text="成都" android:layout_width="wrap_content" android:id="@+id/button3"></RadioButton>
- <RadioButton android:layout_height="wrap_content" android:text="香港" android:layout_width="wrap_content" android:id="@+id/button4"></RadioButton>
- </RadioGroup>
- <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>
- </AbsoluteLayout>
Second.java
|
评论暂时关闭