Android注册布局设计

2017-11-12  本文已影响145人  bbchond
本文仅供参考,老师问问题翻车概不负责

作业的要求是设计一个简易的注册界面,然后实现一些乱起八糟的功能神马的...

如下图:

image.png

那就首先根据该要求写一个布局页面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_margin="10sp"
        android:gravity="center"
        android:textSize="20sp"
        android:text="注册帐号"
        android:textColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <View
        android:layout_width="match_parent"
        android:layout_height="3sp"
        android:background="@android:color/holo_red_light"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:textSize="15sp"
            android:layout_margin="10sp"
            android:text="用  户  名:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/userName"
            android:layout_margin="10sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:textSize="15sp"
            android:layout_margin="10sp"
            android:text="密       码:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/password"
            android:layout_margin="10sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用密码"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:textSize="15sp"
            android:layout_margin="10sp"
            android:text="确认密码:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/surePass"
            android:layout_margin="10sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请再次输入密码"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:textSize="15sp"
            android:layout_margin="10sp"
            android:text="性           别 :"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:id="@+id/sex"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/male"
                android:text="男"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/female"
                android:text="女"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:textSize="15sp"
            android:layout_margin="10sp"
            android:text="爱  好   :"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <CheckBox
            android:layout_margin="10sp"
            android:id="@+id/music"
            android:text="音乐"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <CheckBox
            android:layout_margin="10sp"
            android:id="@+id/sport"
            android:text="运动"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <CheckBox
            android:layout_margin="10sp"
            android:id="@+id/read"
            android:text="阅读"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:textSize="15sp"
            android:layout_margin="10sp"
            android:text="学院:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Spinner
            android:layout_margin="10sp"
            android:id="@+id/college"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </Spinner>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:textSize="15sp"
            android:layout_margin="10sp"
            android:text="系部:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Spinner
            android:layout_margin="10sp"
            android:id="@+id/department"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </Spinner>

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确定"/>
</LinearLayout>

(由于编译器不同,使用Android Studio写布局文件会如下图所示)


image.png

然后便是JAVA代码:

public class MainActivity extends AppCompatActivity {

    private EditText username;
    private EditText password;
    private EditText ensurePass;
    RadioGroup Sex;
    RadioButton male;
    RadioButton female;
    CheckBox music;
    CheckBox sport;
    CheckBox read;
    Spinner college;
    Spinner department;
    Button button;
    String sex;
    String hobby;
    private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
    private String[] collegeName = {"计算机学院","外国语学院","机械学院"};
    private int[] depatrmentName = {R.array.computer,R.array.language,R.array.machine};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer stringBuffer = new StringBuffer();
                String name = username.getText().toString();
                String pass = password.getText().toString();
                String surePass = ensurePass.getText().toString();
                Log.e("Wugui",pass+surePass);
                for (CheckBox checkbox : checkBoxList) {
                    if (checkbox.isChecked()){
                        stringBuffer.append(checkbox.getText().toString() + " ");
                    }
                }

                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass) || TextUtils.isEmpty(surePass)){
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("错误");
                    dialog.setMessage("请输入正确的信息");
                    dialog.setPositiveButton("确定",null);
                    dialog.show();
                } else if (stringBuffer != null && "".equals(stringBuffer.toString())){
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("错误");
                    dialog.setMessage("请选择至少一个爱好");
                    dialog.setPositiveButton("确定",null);
                    dialog.show();
                }
                else {
                    hobby = stringBuffer.toString();
                    showDialog();
                }
            }
        });

        Sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == male.getId()){
                    sex = male.getText().toString();
                } else {
                    sex = female.getText().toString();
                }
            }
        });
    }

    private void showDialog(){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("您的注册信息");
        dialog.setMessage("姓名="+username.getText().toString()+
                "\n" +"密码="+password.getText().toString()+
                "\n"+"性别="+sex+
                "\n"+"爱好="+hobby+
                "\n"+"学院="+college.getSelectedItem().toString()+
                "\n"+"系部="+department.getSelectedItem().toString());
        dialog.setPositiveButton("确定",null);
        dialog.show();
    }

    private void initView(){
        username = (EditText) findViewById(R.id.userName);
        password = (EditText) findViewById(R.id.password);
        ensurePass = (EditText) findViewById(R.id.surePass);
        Sex = (RadioGroup) findViewById(R.id.sex);
        male = (RadioButton) findViewById(R.id.male);
        female = (RadioButton) findViewById(R.id.female);

        music = (CheckBox) findViewById(R.id.music);
        sport = (CheckBox) findViewById(R.id.sport);
        read = (CheckBox) findViewById(R.id.read);
        checkBoxList.add(music);
        checkBoxList.add(sport);
        checkBoxList.add(read);

        college = (Spinner) findViewById(R.id.college);
        department = (Spinner) findViewById(R.id.department);

        ArrayAdapter<String> college_adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item,collegeName);
        college.setAdapter(college_adapter);
        college.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayAdapter<CharSequence> city_adapter = ArrayAdapter.createFromResource(
                        MainActivity.this,depatrmentName[position],android.R.layout.simple_spinner_dropdown_item);
                department.setAdapter(city_adapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}

提示:由于要实现在选择学院时,系部能自动进行联动,因此还需要在values下的strings文件夹里编写如下代码:

    <string-array name="computer">
        <item>计算机科学与技术</item>
        <item>软件工程</item>
        <item>物联网</item>
    </string-array>

    <string-array name="language">
        <item>日语</item>
        <item>英语</item>
        <item>朝鲜语</item>
    </string-array>

    <string-array name="machine">
        <item>汽车</item>
        <item>自动化</item>
        <item>德语</item>
    </string-array>

作业的主要目的就是使用Spinner来进行系部的联动以及对CheckBox进行遍历取值,下面列出这两个部分的CheckBox部分重要代码:
先初始化一个CheckBox的List,如下:

private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();

然后遍历CheckBoxList:

                for (CheckBox checkbox : checkBoxList) {
                    if (checkbox.isChecked()){
                        stringBuffer.append(checkbox.getText().toString() + " ");
                    }
                }

由于List中主要存储了音乐、运动、阅读这三个选项;因此先在onCreate方法内将这三个CheckBox与布局内的控件进行绑定,之后再将这三个CheckBox添加到CheckBoxList中:

        music = (CheckBox) findViewById(R.id.music);
        sport = (CheckBox) findViewById(R.id.sport);
        read = (CheckBox) findViewById(R.id.read);
        checkBoxList.add(music);
        checkBoxList.add(sport);
        checkBoxList.add(read);

然后针对用户没有选择爱好就注册时编写弹出提示框的事件:

                if (stringBuffer != null && "".equals(stringBuffer.toString())){
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("错误");
                    dialog.setMessage("请选择至少一个爱好");
                    dialog.setPositiveButton("确定",null);
                    dialog.show();
                }
image.png

(可以看上面发的全代码看看stringBuffer 是怎么来的);

然后是系部联动部分的代码(在MainActivity的initView方法中可以看到):

        college = (Spinner) findViewById(R.id.college);
        department = (Spinner) findViewById(R.id.department);

        ArrayAdapter<String> college_adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item,collegeName);
        college.setAdapter(college_adapter);
        college.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayAdapter<CharSequence> city_adapter = ArrayAdapter.createFromResource(
                        MainActivity.this,depatrmentName[position],android.R.layout.simple_spinner_dropdown_item);
                department.setAdapter(city_adapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

其中第一个college_adapter主要将collegeName数组中的几个学院的名字放入Spinner控件中,实现了点击Spinner控件下拉展开内容可以显示几个学院;

而college这个Spinner的点击事件则根据点击到的学院名来在系部Spinner中显示对应的系部名;在city_adapter中,使用departmentName[position]来实现了根据选择的学院名展示相应的系部名这一方法。

(Tip:与数组一样,选择Spinner控件的第一个子选项时,position的值为0。)

最后就是一个单击注册之后显示注册信息的提示框咯:

我们先写一个showDialog的方法:

private void showDialog(){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("您的注册信息");
        dialog.setMessage("姓名="+username.getText().toString()+
                "\n" +"密码="+password.getText().toString()+
                "\n"+"性别="+sex+
                "\n"+"爱好="+hobby+
                "\n"+"学院="+college.getSelectedItem().toString()+
                "\n"+"系部="+department.getSelectedItem().toString());
        dialog.setPositiveButton("确定",null);
        dialog.show();
}

然后,在注册button点击事件中进行逻辑判断,当注册信息无误是即可显示该Dialog:

button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer stringBuffer = new StringBuffer();
                String name = username.getText().toString();
                String pass = password.getText().toString();
                String surePass = ensurePass.getText().toString();
                for (CheckBox checkbox : checkBoxList) {
                    if (checkbox.isChecked()){
                        stringBuffer.append(checkbox.getText().toString() + " ");
                    }
                }

                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass) || TextUtils.isEmpty(surePass)){
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("错误");
                    dialog.setMessage("请输入正确的信息");
                    dialog.setPositiveButton("确定",null);
                    dialog.show();
                } else if (stringBuffer != null && "".equals(stringBuffer.toString())){
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("错误");
                    dialog.setMessage("请选择至少一个爱好");
                    dialog.setPositiveButton("确定",null);
                    dialog.show();
                }
                  else {
                    hobby = stringBuffer.toString();
                    showDialog();
                }
            }
        });

至此,一个简单的注册页面以及简单的判断就完成了(建议修改提供的XML源码,将password的EditText的inputType修改为password)。

上一篇下一篇

猜你喜欢

热点阅读