<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id=“@+id/LinearLayout1”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
tools:context=“ r e l a t i v e P a c k a g e . {relativePackage}. relativePackage.{activityClass}” >
<Button
android:id=“@+id/button1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“RadioActivity单选” />
<Button
android:id=“@+id/button2”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“CheckActivity多选” />
MainActivity.java
package com.example.radioandcheckdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
switch (v.getId()) {
case R.id.button1:
//跳转页面
intent.setClass(MainActivity.this, RadioActivity.class);
startActivity(intent);
break;
case R.id.button2:
//跳转页面
intent.setClass(MainActivity.this, CheckActivity.class);
startActivity(intent);
default:
break;
}
}
}
activity_radio.xml
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id=“@+id/LinearLayout1”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:layout_margin=“20sp”
tools:context=“ r e l a t i v e P a c k a g e . {relativePackage}. relativePackage.{activityClass}” >
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@string/hello_world” />
<RadioGroup
android:id=“@+id/group1”
android:orientation=“horizontal”
android:layout_width=“match_parent”
android:layout_height=“wrap_content” >
<RadioButton
android:id=“@+id/radio1”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:checked=“true”
android:text=“男”/>
<RadioButton
android:id=“@+id/radio2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“女”/>
<View
android:layout_width=“match_parent”
android:layout_height=“2sp”
android:background=“@android:color/holo_blue_dark”
android:layout_marginTop=“10sp”
android:layout_marginBottom=“10sp” />
<TextView
android:id=“@+id/text1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:textSize=“18sp”
android:text=“你吃饭了吗?”/>
<RadioGroup
android:id=“@+id/group2”
android:layout_width=“match_parent”
android:layout_height=“wrap_content” >
<RadioButton
android:id=“@+id/radio3”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“吃了”/>
<RadioButton
android:id=“@+id/radio4”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“没吃”/>
RadioActivity.java
package com.example.radioandcheckdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class RadioActivity extends Activity implements OnCheckedChangeListener {
private RadioGroup group1;
private RadioGroup group2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
group1 = (RadioGroup) findViewById(R.id.group1);
group2 = (RadioGroup) findViewById(R.id.group2);
group1.setOnCheckedChangeListener(this);
group2.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//显示值的几种方法
//checkedId选中RadioButton的id
/*switch (checkedId) {
case R.id.radio1:
Toast.makeText(this, “男”, Toast.LENGTH_LONG).show();
break;
case R.id.radio2:
Toast.makeText(this, “女”, Toast.LENGTH_LONG).show();
break;
case R.id.radio3:
Toast.makeText(this, “吃了”, Toast.LENGTH_LONG).show();
break;
case R.id.radio4:
Toast.makeText(this, “没吃”, Toast.LENGTH_LONG).show();
break;
default:
break;
}*/
//找到点击的RadioButton
//RadioButton radio = (RadioButton) findViewById(checkedId);
//取出RadioButton中的值
//String str = radio.getText().toString();
//弹框显示选中的值
//Toast.makeText(this, str, Toast.LENGTH_LONG).show();
//两组数据同时显示
//根据RadioGroup取出数据,没有选中返回-1
String str = “”;
int buttonId = group1.getCheckedRadioButtonId();
if(buttonId != -1){
RadioButton radio = (RadioButton) findViewById(buttonId);
str = “你的性别是” + radio.getText().toString();
}else{
str = “你没有选择性别”;
}
buttonId = group2.getCheckedRadioButtonId();
if(buttonId != -1){
RadioButton radio = (RadioButton) findViewById(buttonId);
str += “, 你吃饭了吗?”+radio.getText().toString();
}
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
}
activity_check.xml
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id=“@+id/LinearLayout1”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
tools:context=“ r e l a t i v e P a c k a g e . {relativePackage}. relativePackage.{activityClass}” >
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“选择所学课程:” />
<CheckBox
android:id=“@+id/check1”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“HTML” />
<CheckBox
android:id=“@+id/check2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“C” />
<CheckBox
android:id=“@+id/check3”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“php” />
<CheckBox
android:id=“@+id/check4”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“java” />
<Button
android:id=“@+id/button1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“提交” />
最后
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
ndroid:text=“java” />
<Button
android:id=“@+id/button1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“提交” />
最后
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术
[外链图片转存中…(img-jcl3FUQF-1714470337455)]
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
[外链图片转存中…(img-bKbkFyAX-1714470337458)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!