Android_Component_example

时间:2023-03-09 03:49:31
Android_Component_example

xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.homework03.MainActivity" > <!-- 第一行:姓名 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="姓名"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入姓名"
android:textSize="20sp" />
</LinearLayout>
<!-- 第二行:密码 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="密码"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入密码"
android:inputType="textPassword"
android:textSize="20sp" />
</LinearLayout>
<!-- 第三行:性别 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="性别"
android:textSize="20sp" /> <RadioGroup
android:id="@+id/group_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" > <RadioButton
android:id="@+id/rb_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" /> <RadioButton
android:id="@+id/rb_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
<!-- 第四行:年龄 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="年龄"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入年龄"
android:inputType="number"
android:textSize="20sp" />
</LinearLayout>
<!-- 第五行:email --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Email"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入Email"
android:inputType="textEmailAddress"
android:textSize="20sp" />
</LinearLayout>
<!-- 第六行:爱好 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="爱好"
android:textSize="20sp" /> <GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="3" > <CheckBox
android:id="@+id/checkbox0"
android:text="动漫" /> <CheckBox
android:id="@+id/checkbox1"
android:text="美食" /> <CheckBox
android:id="@+id/checkbox2"
android:text="约会" /> <CheckBox
android:id="@+id/checkbox3"
android:text="Dota" /> <CheckBox
android:id="@+id/checkbox4"
android:text="篮球" /> <CheckBox
android:id="@+id/checkbox5"
android:text="野炊" /> <CheckBox
android:id="@+id/checkbox6"
android:text="电影" /> <CheckBox
android:id="@+id/checkbox7"
android:text="桌游" /> <CheckBox
android:id="@+id/check_all"
android:text="全选" /> <CheckBox
android:id="@+id/check_none"
android:text="全不选" />
</GridLayout>
</LinearLayout>
<!-- 第七行:评分 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="评分"
android:textSize="20sp" /> <RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="btn_click"
/>
</LinearLayout>

源代码:

package com.example.homework03;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText edit_name, edit_passwd, edit_age, edit_email;
private RadioGroup group_sex;
private CheckBox[] checks;
private CheckBox checkAll, checkNone;
private RatingBar ratingBar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找出对应的控件
edit_name = (EditText) findViewById(R.id.edit_name);
edit_passwd = (EditText) findViewById(R.id.edit_password);
edit_age = (EditText) findViewById(R.id.edit_age);
edit_email = (EditText) findViewById(R.id.edit_email);
group_sex = (RadioGroup) findViewById(R.id.group_sex);
checks = new CheckBox[8];
checks[0] = (CheckBox) findViewById(R.id.checkbox0);
checks[1] = (CheckBox) findViewById(R.id.checkbox1);
checks[2] = (CheckBox) findViewById(R.id.checkbox2);
checks[3] = (CheckBox) findViewById(R.id.checkbox3);
checks[4] = (CheckBox) findViewById(R.id.checkbox4);
checks[5] = (CheckBox) findViewById(R.id.checkbox5);
checks[6] = (CheckBox) findViewById(R.id.checkbox6);
checks[7] = (CheckBox) findViewById(R.id.checkbox7);
checkAll = (CheckBox) findViewById(R.id.check_all);
checkNone = (CheckBox) findViewById(R.id.check_none);
ratingBar = (RatingBar) findViewById(R.id.ratingbar);
ratingBar.setMax(5);
// 给checkbox添加onCheck事件
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) { // 当勾上了全选时,才选择全部的CheckBox
for(CheckBox check : checks) {
check.setChecked(true);
}
checkNone.setChecked(false);
}
}
});
checkNone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) { // 当勾上了全不选时,才取消选择全部的CheckBox
for(CheckBox check : checks) {
check.setChecked(false);
}
checkAll.setChecked(false);
}
}
});
} public void btn_click(View view) {
String result = "";
String name = edit_name.getText().toString();
result += "姓名:" + name +"\n";
String passwd = edit_passwd.getText().toString();
result += "密码:" + passwd +"\n";
int id = group_sex.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(id);
String sex = rb.getText().toString();
result += "性别:" + sex + "\n";
int age = Integer.parseInt(edit_age.getText().toString());
result += "年龄:" + age +"\n";
String email = edit_email.getText().toString();
result += "Email:" + email +"\n"; String favor = "";
for(CheckBox check : checks) {
if(check.isChecked()) {
favor += check.getText().toString() + " ";
}
}
result += "爱好:" + favor +"\n"; int rating = ratingBar.getProgress();
result += "评分:" + rating +"\n"; Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}