欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android实现模拟用户登录

程序员文章站 2022-06-10 13:44:33
...

目录


1. 布局分析
Android实现模拟用户登录
  • 登录主页包含:
    • 输入手机号及密码的输入框EditText
    • 选择性别的单选框RadioGroup
    • 选择爱好的复选框CheckBox
    • 选择城市的下拉菜单Spinner
    • 登录按钮Button
  • 登陆后的页面用于输出用户输入的信息

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">
    
        <LinearLayout
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteY="0dp"
            android:orientation="horizontal"
            tools:layout_editor_absoluteX="8dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:textSize="18sp"
                android:textColor="@android:color/background_dark"
                android:text="手机号:"/>
            <EditText
                android:id="@+id/phone"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:inputType="phone"
                android:hint="请输入手机号"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteY="0dp"
            android:orientation="horizontal"
            tools:layout_editor_absoluteX="8dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:textSize="18sp"
                android:textColor="@android:color/background_dark"
                android:text="密    码:"/>
            <EditText
                android:id="@+id/paswd"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:inputType="numberPassword"
                android:hint="请输入密码"/>
        </LinearLayout>
        <RadioGroup
            android:id="@+id/sex"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/nan"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text=""/>
            <RadioButton
                android:id="@+id/nv"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text=""/>
        </RadioGroup>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <CheckBox
                android:id="@+id/read_book"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="读书" />
            <CheckBox
                android:id="@+id/play_ball"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打球" />
            <CheckBox
                android:id="@+id/music"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="听音乐" />
        </LinearLayout>
        <Spinner								# 下拉菜单Spinner中的菜单项在java中实现
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/register"
            android:layout_width="fill_parent"
            android:background="#39c5bb"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            android:text="注 册"
            android:layout_height="40dp" />
    </LinearLayout>
    
    • 这部分代码相信大家都看得懂,就不多做注释了

3. 功能实现
  • MainActivity.java
    public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener
    {
        private String mytel="";
        private String mypswd="";
        private String mysex="男";
        private String myhobby="";
        private String mycity="";
    
    	//注册控件用于管理xml中对应控件
        EditText tel,pswd;
        RadioGroup sexs;
        RadioButton man,woman;
        CheckBox read,play,music;
        Button register;
        Spinner cities;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    		//关联xml中的控件
            tel     = (EditText)findViewById(R.id.phone);
            pswd    = (EditText)findViewById(R.id.paswd);
            sexs     = (RadioGroup)findViewById(R.id.sex);
            man     = (RadioButton)findViewById(R.id.nan);
            sexs.setOnCheckedChangeListener(this);					//设置CheckBox监听
            read    = (CheckBox)findViewById(R.id.read_book);
            play    = (CheckBox)findViewById(R.id.play_ball);
            music   = (CheckBox)findViewById(R.id.music);
            register    = (Button)findViewById(R.id.register);
            register.setOnClickListener(this);						//设置Button监听
            cities    =(Spinner)findViewById(R.id.spinner);
            final String[] citylist = new String[]{"北京","上海","重庆","广州","武汉"};		//设置下拉菜单Spinner的菜单项
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,citylist);
            cities.setAdapter(adapter);
            cities.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){			//设置Spinner监听
    
                @Override																	//Spinner监听勾选选项
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    mycity=citylist[pos];
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
    
                }
            });
        }
        @Override
        public void onClick(View view){				//监听点击事件
            switch (view.getId()){
                case R.id.register:							//登录按钮点击事件
                mytel = tel.getText().toString();				//读取EditText输入信息
                mypswd = pswd.getText().toString();
                myhobby = "";
                if(read.isChecked()){						//读取CheckBox
                    myhobby += read.getText().toString();
                }
                if(play.isChecked()){
                    myhobby += play.getText().toString();
                }
                if(music.isChecked()){
                    myhobby += music.getText().toString();
                }
    
                Intent intent = new Intent(this,show.class);		//Intent跳转到Activity: show
                Bundle bundle = new Bundle();						//数据传递容器Bundle
                bundle.putString("tel",mytel);						//以键值对的方式存储数据
                bundle.putString("pswd",mypswd);
                bundle.putString("sex",mysex);
                bundle.putString("hobby",myhobby);
                bundle.putString("city",mycity);
                intent.putExtras(bundle);							//将数据容器放入Intent进行传递
                startActivity(intent);								//执行跳转
                break;
            }
        }
        @Override
        public void onCheckedChanged(RadioGroup group, int i) {
            mysex = i == R.id.nan?"男性":"女性";
        }
    }
    

4. 信息显示
  • 在MainActivity中,我们通过Intent intent = new Intent(this,show.class);startActivity(intent);实现了跳转到Activity:show,并把数据通过Bundle传递了过来
  • show.java
    public class show extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show);
            
            Intent intent=this.getIntent();				//获取Intent
            Bundle bundle=intent.getExtras();			//获取Bundle
            String tel=bundle.getString("tel");			//以键值对的方式读取Bundle里的信息
            String pswd=bundle.getString("pswd");
            String sex=bundle.getString("sex");
            String hobby=bundle.getString("hobby");
            String city=bundle.getString("city");
            TextView show_text=(TextView)findViewById(R.id.show);	//关联到xml的show组件
            //将读取到的信息发送到xml的show组件
            show_text.setText("手机号:"+tel+"\n"+"密码:"+pswd+"\n"+"性别:"+sex+"\n"+"爱好:"+hobby+"\n"+"城市:"+city+"\n");
        }
    
    }
    
    • 这里将数据读取出来后输出到了activity_show.xml里的show组件
  • activity_show.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"
        tools:context=".show">
    
        <TextView
            android:id="@+id/show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"/>
    </LinearLayout>
    

5. 代码说明
  • 这次简单介绍了界面元素 ButtonEditTextRadioGroupCheckBoxSpinner 和实现了不同Activity和xml之间的数据交互的 Bundle,大家可以研究研究这份代码,自己改一改看看会有什么效果。
  • Github仓库链接