android studio如何使用RadioGroup,并且选重任意radiobutto
单选控件实现 |
实验图:
android studio如何使用RadioGroup,并且选重任意radiobutto
MainActivity.java
package com.example.b_boy2;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "TAG";
private int choose;
private RadioButton radioButton;
private boolean dataWindowShow = true;
private RadioGroup radioGroup;
private RadioButton class5, class1, class2, class3, class4;
private Button classBtn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
class5 = (RadioButton) findViewById(R.id.radioButton5);
class4 = (RadioButton) findViewById(R.id.radioButton4);
class3 = (RadioButton) findViewById(R.id.radioButton3);
class2 = (RadioButton) findViewById(R.id.radioButton2);
class1 = (RadioButton) findViewById(R.id.radioButton1);
classBtn = (Button) findViewById(R.id.btn_class);
classBtn.setOnClickListener(this);
class5.setEnabled(true);
class4.setEnabled(true);
class3.setEnabled(true);
class2.setEnabled(true);
class1.setEnabled(true);
radioGroup = (RadioGroup) findViewById(R.id.rb2);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
choose = checkedId;
radioButton = (RadioButton) group.findViewById(checkedId);
Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_class:
if (radioButton.getText().equals("class1")) {
} else if (radioButton.getText().equals("class2")) {
} else if (radioButton.getText().equals("class3")) {
} else if (radioButton.getText().equals("class4")) {
} else if (radioButton.getText().equals("选择模型")) {
Toast.makeText(MainActivity.this, "未选择", Toast.LENGTH_SHORT).show();
}
class5.setChecked(true);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:background="@color/cornsilk"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="463dp">
<ImageView
android:id="@+id/iv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="457dp"
android:layout_gravity="end"
android:orientation="vertical">
<LinearLayout
android:id="@+id/image11"
android:layout_width="319dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="51dp"
android:background="@drawable/data_window"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/class1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="319dp"
android:layout_height="102dp"
android:layout_weight="1"
app:srcCompat="@drawable/class2" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/class3" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/class4" />
</LinearLayout>
<RadioGroup
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="225dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="69dp"
android:layout_height="81dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@animator/shape"
android:button="@null"
android:gravity="center"
android:text="class1"
android:textColor="#000000"
android:textSize="20sp"
tools:text="class1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="79dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@animator/shape"
android:button="@null"
android:gravity="center"
android:text="class2"
android:textColor="#000000"
android:textSize="20sp"
tools:text="class2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="81dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@animator/shape"
android:button="@null"
android:gravity="center"
android:text="class3"
android:textColor="#000000"
android:textSize="20sp"
tools:text="class3" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="84dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@animator/shape"
android:button="@null"
android:gravity="center"
android:text="class4"
android:textColor="#000000"
android:textSize="20sp"
tools:text="class4" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="79dp"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="@animator/shape"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="选择模型"
android:textColor="@color/black"
android:textSize="20sp" />
</RadioGroup>
<Button
android:id="@+id/btn_class"
android:layout_width="368dp"
android:layout_height="146dp"
android:layout_weight="1"
android:text="分类抓取" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
【关注微信公众号一起来交流】 |