当前位置: 首页 > 工具软件 > RadioGroup > 使用案例 >

radioGroup

百里俭
2023-12-01

package com.example.radiogroup_06;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private TextView textView;
private RadioGroup radioGroup;
private RadioButton radioBtn01;
private RadioButton radioBtn02;
private RadioButton radioBtn03;
private RadioButton radioBtn04;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.text);
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioBtn01 = (RadioButton) findViewById(R.id.radio_btn01);
radioBtn03 = (RadioButton) findViewById(R.id.radio_btn02);
radioBtn03 = (RadioButton) findViewById(R.id.radio_btn03);
radioBtn04 = (RadioButton) findViewById(R.id.radio_btn04);

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group,int checkedId){
if(checkedId==radioBtn03.getId()){
displayToast("正確答案:"+radioBtn03.getText()+"\n"+"恭喜您!回答正確");
}else{
displayToast("回答錯誤");
}
}


});
}


public void displayToast(String string){
Toast toast = Toast.makeText(this, string, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM, 0, 220);
toast.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_btn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_btn01"/>
<RadioButton
android:id="@+id/radio_btn02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_btn02"/>
<RadioButton
android:id="@+id/radio_btn03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_btn03"/>
<RadioButton
android:id="@+id/radio_btn04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_btn04"/>
</RadioGroup>

</LinearLayout>

 

转载于:https://www.cnblogs.com/Smart-Du/p/4302000.html

 类似资料: