android学习日常3-15 关于单选框附加监听的复习
16lz
2022-01-24
MainActivity中代码
package com.mz.dx;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast;public class MainActivity extends Activity { private RadioGroup sex_radioGroup; private Button btn_s; /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sex_radioGroup=(RadioGroup)findViewById(R.id.sex_ridioGroup); sex_radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup arg0, int arg1) { // TODO Auto-generated method stub if(arg1==R.id.sex_man){ Toast toast=Toast.makeText(MainActivity.this, "您选中的是男", Toast.LENGTH_LONG); toast.show(); }else{ Toast toast=Toast.makeText(MainActivity.this, "您选中的是女", Toast.LENGTH_LONG); toast.show(); } } }); btn_s=(Button)findViewById(R.id.btn_s); btn_s.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub for(int i=0;iif(radio.isChecked()){ Toast toast=Toast.makeText(MainActivity.this,"您选中的是:"+radio.getText(),Toast.LENGTH_SHORT); toast.show(); } } } }); }}
前端代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="选择性别"/> <RadioGroup android:id="@+id/sex_ridioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/sex_man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男"/> <RadioButton android:id="@+id/sex_woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> RadioGroup> <Button android:id="@+id/btn_s" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交"/>LinearLayout>
解析未完。。。
更多相关文章
- android radiobutton选中字体颜色改变的方法
- android点击空白区域隐藏软键盘的实现
- Android中获取系统的时间
- Android(安卓)drawText 纵向居中
- 【Android】viewpager banner 广告 自动轮播 小圆点
- android 打开新浪微博代码
- Android(安卓)系统是否要重启时弹出框
- Android中两个控件滑动效果冲突的解决办法
- Android(安卓)获取电池基本信息代码