布局,控件设置
<TextView android:id="@+id/tv_show" android:layout_width="177dp" android:layout_height="37dp" android:layout_marginBottom="1dp" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.545" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.569" style="@style/textstyle02"/> <Button android:id="@+id/button_test" android:layout_width="88dp" android:layout_height="48dp" android:layout_marginStart="104dp" android:layout_marginLeft="104dp" android:layout_marginTop="96dp" android:layout_marginEnd="192dp" android:layout_marginRight="192dp" android:text="点我试试" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv_show" />
<RadioGroup android:id="@+id/gp" android:layout_width="108dp" android:layout_height="172dp" android:layout_marginStart="40dp" android:layout_marginLeft="40dp" android:layout_marginTop="85dp" app:layout_constraintStart_toEndOf="@+id/checkBox2" app:layout_constraintTop_toTopOf="parent"> <RadioButton android:id="@+id/radioButtona" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="A." /> <RadioButton android:id="@+id/radioButtonb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="B." /> <RadioButton android:id="@+id/radioButtonc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="c" /> <RadioButton android:id="@+id/radioButtond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="d" /> </RadioGroup> <TextView android:id="@+id/textView2" android:layout_width="211dp" android:layout_height="30dp" android:layout_marginStart="85dp" android:layout_marginLeft="85dp" android:layout_marginTop="25dp" android:layout_marginBottom="25dp" android:text="选一个吧" app:layout_constraintBottom_toTopOf="@+id/checkBox1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <SlidingDrawer android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:content="@+id/myview" android:handle="@+id/layout1" android:orientation="horizontal"> <!--引导按钮--> <LinearLayout android:id="@+id/layout1" android:layout_width="35dp" android:layout_height="match_parent" android:gravity="center"> <ImageView android:id="@+id/myimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher_background"/> </LinearLayout> <!--内容--> <ImageView android:id="@+id/myview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher_background" />
功能实现 public class MainActivityete extends AppCompatActivity { //Button button=(Button)findViewById(R.id.button_test); //TextView tv_show=(TextView)findViewById(R.id.tv_show); CheckBox cb1; CheckBox cb2; CheckBox cb3; CheckBox cb4; RadioGroup rg; int mScreenWith; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout myLayout=new LinearLayout(this); // myLayout.setOrientation(LinearLayout.VERTICAL); myLayout.setBackgroundResource(R.drawable.ic_launcher_background); setTitle("checkactiv"); setContentView(R.layout.activity_main_activityete); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); Button button=(Button)findViewById(R.id.button_test); final TextView tvshow=(TextView)findViewById(R.id.tv_show); rg=(RadioGroup) findViewById(R.id.gp); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str="点击事件"; tvshow.setText("你不要过来压~"); tvshow.setTextColor(Color.YELLOW); Toast.makeText(MainActivityete.this,"啊~~~",Toast.LENGTH_SHORT).show(); for(int i=0;i<rg.getChildCount();i++) { RadioButton rb=(RadioButton)rg.getChildAt(i); if(rb.isChecked()) { if(rb.getText().equals("B.")) { Toast.makeText(MainActivityete.this,"对",Toast.LENGTH_SHORT).show(); }else { AlertDialog.Builder bud=new AlertDialog.Builder(MainActivityete.this); bud.setMessage("再试一次"); bud.setPositiveButton("好",null).show(); } break; } } } }); }
格式资源 res/values/style.xml
<resources> ... <style name="styletext01"> <item name="android:textSize">16sp</item> <item name="android:textColor">#FFFFFF</item> </style> <style name="textstyle02"> <item name="android:textSize">20sp</item> <item name="android:textColor">#fd8d8d</item> <item name="android:fromAlpha">0.0</item> <item name="android:toAlpha">0.0</item> </style> </resources>