Android simple experiment two events

A: renderings

Two: layout code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:background="#D3D3D3">

    <RelativeLayout
        android:id="@+id/id1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/top_bg">
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:text="@string/title"
            android:textSize="20sp"
            android:gravity="center_vertical"
            />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/id2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        android:layout_below="@+id/id1">
        <ImageView
            android:id="@+id/profile"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="@drawable/profile"/>

        <RadioGroup
            android:id="@+id/name2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal"
            android:checkedButton="@id/student">

            <RadioButton
                android:id="@+id/student"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:text="@string/student"/>

            <RadioButton
                android:id="@+id/work"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:text="@string/work"/>
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/name1"
        android:layout_width="match_parent"
        android:layout_height="400sp"
        android:orientation="vertical"
        android:layout_below="@+id/id2"
        android:padding="10dp"
        android:layout_marginTop="10dp">

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:hint="@string/username"
            android:padding="10dp"
            android:textSize="15dp"
            android:singleLine="true"
            android:paddingLeft="10dp"
            android:background="@drawable/single_edittext"/>

        <EditText
            android:id="@+id/phone"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:hint="@string/phone"
            android:padding="10dp"
            android:textSize="15dp"
            android:singleLine="true"
            android:paddingLeft="10dp"
            android:inputType="phone"
            android:layout_marginTop="10dp"
            android:background="@drawable/top_edittext"/>

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:hint="@string/password"
            android:padding="10dp"
            android:textSize="15dp"
            android:singleLine="true"
            android:paddingLeft="10dp"
            android:layout_marginTop="1dp"
            android:inputType="textPassword"
            android:background="@drawable/bottom_edittext"/>
        <!--<EditText-->
            <!--android:id="@+id/sex"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="45dp"-->
            <!--android:hint="选择性别"-->
            <!--android:layout_marginTop="10dp"-->
            <!--android:background="@drawable/bottom_edittext"/>-->
        <!--<EditText-->
            <!--android:id="@+id/hobby1"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="45dp"-->
            <!--android:hint="兴趣"-->
            <!--android:background="@drawable/bottom_edittext"/>-->

        <LinearLayout
        android:id="@+id/id3"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:orientation="horizontal"
        android:padding="10dp"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/hobby"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:textSize="15sp"
            android:text="@string/hobby"
            android:gravity="center_vertical"/>

        <CheckBox
            android:id="@+id/program"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:text="@string/program"
            android:textSize="15sp"/>

        <CheckBox
            android:id="@+id/swim"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:text="@string/swim"
            android:textSize="15sp"/>

        <CheckBox
            android:id="@+id/run"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:text="@string/run"
            android:textSize="15sp"/>
    </LinearLayout>
        <Button
            android:id="@+id/register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/register"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:background="@drawable/dialog_up"/>
        <TextView
            android:id="@+id/msg_show"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</RelativeLayout>

三:Java代码

public class MainActivity extends AppCompatActivity {
    EditText etPhone = null;
    EditText etPassword = null;
    RadioButton rbStudent = null;
    RadioButton rbWork = null;
    CheckBox cbProgram = null;
    CheckBox cbSwim = null;
    CheckBox cbRun  = null;
    Button btRegister = null;
    TextView tvShow = null;
    EditText etName = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName = (EditText)findViewById(R.id.username);
        etPhone = (EditText)findViewById(R.id.phone);
        etPassword = (EditText)findViewById(R.id.password);
        rbStudent = (RadioButton)findViewById(R.id.student);
        rbWork = (RadioButton)findViewById(R.id.work);
        cbProgram = (CheckBox)findViewById(R.id.program);
        cbSwim = (CheckBox)findViewById(R.id.swim);
        cbRun = (CheckBox)findViewById(R.id.run);
        btRegister = (Button)findViewById(R.id.register);
        tvShow = (TextView)findViewById(R.id.msg_show);
        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                String sName = etName.getText().toString();
                String s = etPhone.getText().toString();
                String p = etPassword.getText().toString();
                if (TextUtils.isEmpty(sName)){
                    Toast.makeText(MainActivity.this,"用户名不能为空!",
                            Toast.LENGTH_LONG).show();
                    return;
                }
                if(s.length() !=11 ){
                    Toast.makeText(MainActivity.this,"号码不正确!",
                            Toast.LENGTH_LONG).show();
                    return;
                }
                tvShow.setText("用户名:"+sName+("\n"));
                tvShow.append("手机号:"+s+("\n"));
                tvShow.append("密码:"+p+("\n"));
                tvShow.append("你的身份是:");
                if(rbWork.isChecked()){
                    tvShow.append("已工作"+("\n"));
                }
                if (rbStudent.isChecked()){
                    tvShow.append("学生"+("\n"));
                }
                tvShow.append("你的爱好是:");
                if(cbProgram.isChecked()) {
                    tvShow.append("编程,");
                }
                if (cbSwim.isChecked()){
                    tvShow.append("游泳,");
                }
                if (cbRun.isChecked()){
                    tvShow.append("跑步");
                }
            }
        });
    }
}

分析:实现同一个布局的方法有很多种,所以以上布局仅供参考。

Java代码思路:首先获取到三个输入框里面的字符串,然后直接使用if语句对其进行判断,判断所获取到的字符串是否为空,如果为空就弹出提示框Toast,提示便可以了;如果不为空,那就什么也不用管。判断手机号码是否为11位数时也一样,但是如果在布局那里给手机号码输入框的inputType是phone类型,那么就只能输入11位数了。选择兴趣时,判断这个兴趣前面对应的checkBox是否✔便可以了,如果✔了就显示出来,如果没有✔就不用管。

发布了28 篇原创文章 · 获赞 5 · 访问量 5807

Guess you like

Origin blog.csdn.net/weixin_41879980/article/details/95716572