前进之路 发表于 2022-6-25 00:35:58

Android开发--实现Android登录注册页面(上)

简单的Android开发登录注册,这个是没有连数据库的
首先,新建项目,新建一个登录页面LoginActivity和注册页面RegisterActivity。
下面是登录页面的代码:activity_login.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=".LoginActivity">
    <LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:orientation="horizontal"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginLeft="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:gravity="center_vertical">

<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><TextView
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:text="账号:"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:textSize="25sp" />

<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><EditText
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:id="@+id/et_account"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:hint="请输入用户名或手机号"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    style="@style/MyEditStyle"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_marginLeft="10dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:inputType="text"></EditText>
    </LinearLayout>

    <LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:orientation="horizontal"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginLeft="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:gravity="center_vertical">

<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><TextView
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:text="密码:"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:textSize="25sp" />

<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><EditText
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:id="@+id/et_password"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:hint="请输入密码"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    style="@style/MyEditStyle"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_marginLeft="10dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:inputType="numberPassword"></EditText>
    </LinearLayout>
    <LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:orientation="horizontal"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:gravity="center"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginLeft="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>>
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><CheckBox
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:id="@+id/cb_remember"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:text="记住密码"></CheckBox>
    </LinearLayout>
    <Button
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:id="@+id/btn_login"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:text="登录"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>style="@style/MyBtnStyle"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginLeft="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"></Button>
    <TextView
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:textColor="@color/colorPrimary"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:text="还没有账号?"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_gravity="right"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="10dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:onClick="toRegister"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>></TextView>

</LinearLayout> 效果如图:https://img-blog.csdnimg.cn/65cc785861c143168c114992946888d6.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP6bm_6bm_wrA=,size_12,color_FFFFFF,t_70,g_se,x_16



下面是注册页面的代码:activity_register.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=".LoginActivity">
    <LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:orientation="horizontal"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginLeft="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:gravity="center_vertical">

<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><TextView
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:text="账号:"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:textSize="25sp" />

<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><EditText
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:id="@+id/et_account"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:hint="请输入用户名或手机号"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    style="@style/MyEditStyle"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_marginLeft="10dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:inputType="text"></EditText>
    </LinearLayout>

    <LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:orientation="horizontal"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginLeft="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:gravity="center_vertical">

<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><TextView
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:text="密码:"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:textSize="25sp" />

<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><EditText
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:id="@+id/et_password"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:hint="请输入密码"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    style="@style/MyEditStyle"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_marginLeft="10dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:inputType="numberPassword"></EditText>
    </LinearLayout>
    <LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:orientation="horizontal"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:gravity="center"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginLeft="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>>
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources><CheckBox
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:id="@+id/cb_remember"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>    android:text="记住密码"></CheckBox>
    </LinearLayout>
    <Button
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:id="@+id/btn_login"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:text="登录"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>style="@style/MyBtnStyle"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginLeft="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"></Button>
    <TextView
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:textColor="@color/colorPrimary"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:text="还没有账号?"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_gravity="right"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginRight="20dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:layout_marginTop="10dp"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>android:onClick="toRegister"
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>></TextView>

</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources> 效果如图:https://img-blog.csdnimg.cn/021a0f4fff384cb593bee7aa32a64ab4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP6bm_6bm_wrA=,size_12,color_FFFFFF,t_70,g_se,x_16


最后,在LoginActivity.java中,加入一串代码:getSupportActionBar().setTitle("登录");
在RegisterActivity.java中,加入一串代码:getSupportActionBar().setTitle("注册");
public class LoginActivity extends AppCompatActivity {

    public static final int REQUEST_CODE_REGISTER = 1;
    private static final String TAG="tag";
    private Button btnLogin;
    private EditText etAccount,etPassword;
    private CheckBox cbRemember;
    private String userName="a";
    private String pass="123";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>super.onCreate(savedInstanceState);
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>setContentView(R.layout.activity_login);
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>getSupportActionBar().setTitle("登录");
   }
}  上面的代码是LoginActivity.java中的代码,其实要添加的只有一句,其他都写出来是为了让读者能看懂,具体写在哪里。这段代码的作用是: 使标题栏那边显示的文字是登录/注册,而不是一串默认的英文
下面两张图片,左边是未添加代码的效果,右边是添加代码后的效果
https://img-blog.csdnimg.cn/230f18e96dd941559ac93718d08f229c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP6bm_6bm_wrA=,size_12,color_FFFFFF,t_70,g_se,x_16https://img-blog.csdnimg.cn/be4c0268a19b47c69856efa573c87d5f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP6bm_6bm_wrA=,size_15,color_FFFFFF,t_70,g_se,x_16

写到这里,页面整体布局大致完成了,下面,你们需要添加部分细节,来使得颜色和样式跟我一致。我在上面的xml文件代码中,有下面这个代码:
style="@style/MyEditStyle"和style="@style/MyBtnStyle" 你们在前面写的时候,可能会报错,很正常,因为这是引用控件样式的代码,你们需要设置一下这个样式,然后引用,就不会报错啦。
这是设置EditText和Button控件的样式,首先新建style.xml文件。在values中右键,New--Values Resource File,文件名为style。在style.xml文件中,写如下代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>  这里面又引用了样式,是设置输入框的边框和按钮的背景颜色。继续下面的步骤:drawable右键--New--Drawable Resource File,文件名为:btn_bg_selector,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources>  drawable右键--New--Drawable Resource File,文件名为:edit_text_bg,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources> 最后,再调整一下导航栏和标题栏的颜色,就OK啦
colors.xml文件中,添加绿色这个颜色,如下代码,
    <color name="colorPrimary">@color/green_500</color>
    <color name="colorPrimaryDark">@color/green_700</color>
    <color name="colorAccent">#E64A19</color>

    <color name="green_200">#A5D6A7</color>
    <color name="green_500">#4CAF50</color>
    <color name="green_700">#4CAF50</color> themes.xml文件中,只需要修改前两个item的内容,其他代码是为了你们参照一下位置,别改错了。代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>

   

   

</resources> 到这里,你的登录注册页面部分就完成啦!恭喜你!
以上代码,都是我自己一个个敲的,也运行过,运行结果是没问题的。当然如果你有什么意见建议,请不吝赐教❥(^_-)
具体的跳转实现看下一篇笔记哦,下一篇笔记结尾也有源码分享。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Android开发--实现Android登录注册页面(上)