在Android中实现点击按钮后,将页面的单选性别和多选爱好信息日志打印出来,要求有完整的xml文件和java文件
时间: 2024-02-22 16:57:38 浏览: 104
好的,这里是实现上述功能的完整xml和java文件的示例代码:
activity_main.xml:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:" />
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_gender"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<TextView
android:id="@+id/tv_hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rg_gender"
android:text="爱好:" />
<CheckBox
android:id="@+id/cb_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_hobby"
android:text="运动" />
<CheckBox
android:id="@+id/cb_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_hobby"
android:layout_toRightOf="@id/cb_sport"
android:text="音乐" />
<CheckBox
android:id="@+id/cb_reading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_hobby"
android:layout_toRightOf="@id/cb_music"
android:text="阅读" />
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cb_sport"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="提交" />
</RelativeLayout>
```
MainActivity.java:
```java
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RadioGroup rgGender;
private RadioButton rbMale;
private RadioButton rbFemale;
private CheckBox cbSport;
private CheckBox cbMusic;
private CheckBox cbReading;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgGender = findViewById(R.id.rg_gender);
rbMale = findViewById(R.id.rb_male);
rbFemale = findViewById(R.id.rb_female);
cbSport = findViewById(R.id.cb_sport);
cbMusic = findViewById(R.id.cb_music);
cbReading = findViewById(R.id.cb_reading);
btnSubmit = findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 获取性别和爱好信息
String gender = "";
if (rbMale.isChecked()) {
gender = "男";
} else if (rbFemale.isChecked()) {
gender = "女";
}
StringBuilder hobbies = new StringBuilder();
if (cbSport.isChecked()) {
hobbies.append("运动 ");
}
if (cbMusic.isChecked()) {
hobbies.append("音乐 ");
}
if (cbReading.isChecked()) {
hobbies.append("阅读 ");
}
// 打印日志
Log.d(TAG, "onClick: 性别为 " + gender + ",爱好为 " + hobbies.toString());
}
});
}
}
```
在上面的示例中,我们通过 findViewById() 方法获取到了页面中的单选性别和多选爱好控件,并在按钮的点击事件中获取了这些控件的选中状态,并将其打印出来。注意,在打印日志时,我们使用了 Log.d() 方法,并指定了一个标签,以便于在日志中区分不同类型的信息。
阅读全文
相关推荐


















