当前位置: 代码迷 >> 综合 >> Android Studio入门day04(RadioButton)
  详细解决方案

Android Studio入门day04(RadioButton)

热度:9   发布时间:2024-02-20 10:34:50.0

RadioButton

一. 在myselfstudy0909.xml中添加一个Button按钮,默认小写

android:textAllCaps="false"
    <Buttonandroid:id="@+id/btn_radiobutton0919"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="学习RadioButton"android:textAllCaps="false"/>

二. 创建Empty_Activity,名称redioButton0919

三. 在activity_redio_button0919中:使用相对布局,在RadioGroup中包裹RadioButton,只可选择其一按钮

<RadioGroupandroid:id="@+id/rg_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:textColor="#FF6600"><RadioButtonandroid:id="@+id/rb_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="愛"android:textSize="20sp"android:textColor="#FF6600"/><RadioButtonandroid:id="@+id/rb_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="不爱"android:textSize="20sp"android:textColor="#FF6600"android:checked="true"/></RadioGroup><RadioGroupandroid:id="@+id/rg_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:textColor="#FF6600"android:orientation="horizontal"android:layout_marginTop="100dp"><RadioButtonandroid:id="@+id/rb_3"android:layout_width="80dp"android:layout_height="40dp"android:gravity="center"android:text="愛"android:textSize="20sp"android:textColor="#FF6600"android:button="@null"android:layout_marginLeft="20dp"android:background="@drawable/selector_rediobutton"/><RadioButtonandroid:id="@+id/rb_4"android:layout_width="80dp"android:layout_height="40dp"android:gravity="center"android:text="不爱"android:textSize="20sp"android:textColor="#FF6600"android:checked="true"android:layout_marginLeft="20dp"android:background="@drawable/selector_rediobutton"/></RadioGroup>

四. 整合testView.java后端代码,封装出一个类DefineOnclick,代码如下:

// 定义点击事件,每次只需调用方法setListeners()在其中形参使用该类的对象 
private class DefineOnclick implements View.OnClickListener{ @Overridepublic void onClick(View view) {Intent intent = null;switch (view.getId()){case R.id.btn_radiobutton0919:intent = new Intent(testView.this,redioButton0919.class);break;case R.id.btn_textview:intent = new Intent(testView.this,jumpAftertestView.class);break;case R.id.btn_photoview:intent = new Intent(testView.this,jumpAfterphotoView.class);break;case R.id.btn_login:intent = new Intent(testView.this,onclickLogin_jump.class);break;case R.id.btn_dynamicTest:intent = new Intent(testView.this,nj_learn_textview.class);break;}startActivity(intent); // 跳转}}

封装一个方法,参数保存点击事件类的对象,代码如下:

// 设置监听,只需调用一次监听方法,将定义点击事件类的对象传入形式参数中
private void setListeners(){ DefineOnclick defineOnclick = new DefineOnclick();BtnRadioButton0919.setOnClickListener(defineOnclick);BtnTextView.setOnClickListener(defineOnclick);BtnPhotoView.setOnClickListener(defineOnclick);BtnLogin.setOnClickListener(defineOnclick);Btndynamic.setOnClickListener(defineOnclick);}

五. 只需在重写方法中通过id找到视图,并且设置监听,即能自动创建点击事件的对象,体现出java封装性的特点

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.myselfstudy0909);BtnTextView = (Button)findViewById(R.id.btn_textview);BtnPhotoView = (Button)findViewById(R.id.btn_photoview);BtnLogin = findViewById(R.id.btn_login);Btndynamic = findViewById(R.id.btn_dynamicTest);BtnRadioButton0919 = findViewById(R.id.btn_radiobutton0919);// 封装注释此语句关键 创建类 创建方法(其中调用类的对象)setListeners();}

 

 

 

 

 

 

 

 

 

 

 

  相关解决方案