27.02.2014 Views

Android 开发教程

Android 开发教程

Android 开发教程

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

-----------------------------------<strong>Android</strong> 编 程 基 础<br />

4 修 改 mainActivity.java 文 件<br />

package zyf.Ex_Ctrl_6;<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.widget.RadioButton;<br />

import android.widget.RadioGroup;<br />

import android.widget.TextView;<br />

public class Ex_Ctrl_6 extends Activity {<br />

/** Called when the activity is first created. */<br />

private TextView answer_TextView;<br />

private RadioButton boy_RadioButton,girl_RadioButton;<br />

private RadioGroup radioGroup;<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.main);<br />

/* findViewById() 从 XML 中 获 取 资 源 对 象 */<br />

answer_TextView=(TextView)findViewById(R.id.TextView_Ask_And_Show);<br />

radioGroup=(RadioGroup)findViewById(R.id.RadioGroup);<br />

boy_RadioButton=(RadioButton)findViewById(R.id.RadioButton_Boy);<br />

girl_RadioButton=(RadioButton)findViewById(R.id.RadioButton_Gril);<br />

/* 给 单 RadioGroup 添 加 状 态 改 变 监 听 器 */<br />

radioGroup.setOnCheckedChangeListener(new<br />

new<br />

RadioGroup.OnCheckedChangeListener(){<br />

}<br />

}<br />

@Override<br />

public void onCheckedChanged(RadioGroup group, int checkedId) {<br />

// TODO Auto-generated method stub<br />

if(boy_RadioButton.isChecked()){<br />

answer_TextView.setText(R.string.iam_Boy);<br />

}else<br />

else{<br />

answer_TextView.setText(R.string.iamGirl);<br />

}<br />

}<br />

});<br />

25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!