3分钟
2.1.2 实施步骤
实施步骤
任务完成后的运行效果如图:
步骤1:新建一个Module,命名为Ex2_1_2,其他设置默认。
步骤2:修改布局文件,清单如下。
表2-1-3 Ex2_1_2 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="24sp"
android:text="用户注册" />
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:textSize="18sp"
android:text="用户名:"/>
<EditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:textSize="18sp"
android:text="密 码:"/>
<EditText
android:id="@+id/etPasswords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:textSize="18sp"
android:text="性 别:"/>
<RadioGroup
android:id="@+id/rgGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1" >
<RadioButton
android:id="@+id/rbMan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_weight="1"
android:text="男" />
<RadioButton
android:id="@+id/rbWoman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_weight="1"
android:text="女" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:text="确定" />
<Button
android:id="@+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:text="重置" />
</LinearLayout>
</LinearLayout>
步骤3:编写MainActivity.java文件。清单如下:
表2-1-4 Ex2_1_2 MainActivity.java 清单
public class MainActivity extends AppCompatActivity {
//将每一个需要参与计算的控件都用一个变量指定。建议变量名称和ID相同,
//便于记忆。
EditText etUserName,etPasswords;
RadioButton rbMan,rbWoman;
Button btnOK,btnReset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
btnOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str="你输入的是:\n";
str+="用户名:"+etUserName.getText().toString()+"\n";
str+="密码:"+etPasswords.getText().toString()+"\n";
if(rbMan.isChecked()) {
str += "性别:" + rbMan.getText().toString() + "\n";
}
if(rbWoman.isChecked()){
str += "性别:" + rbWoman.getText().toString();
}
Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
}
});
}
/**
* 初始化成员变量,指向界面上的对应控件
*/
private void initUI() {
etUserName = (EditText)findViewById(R.id.etUserName);
etPasswords = (EditText)findViewById(R.id.etPasswords);
rbMan = (RadioButton)findViewById(R.id.rbMan);
rbWoman = (RadioButton)findViewById(R.id.rbWoman);
btnOK = (Button)findViewById(R.id.btnOK);
btnReset = (Button)findViewById(R.id.btnReset);
}
}
学员评价