一、界面设置
1、创建项目,选择EmptyActivity
2、新建空白的Activity,名为Login_Activity,设置为启动界面
a.方法一:创建时直接选择设置为启动界面(勾选LauncherActivity)
b.方法二:手动配置AndroidManifest.xml文件,注册该活动为启动界面
<activity android:name=".Login_Activity" android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3、为该活动创建对应的布局文件,在layout文件夹中新建activity_login.xml文件
4、设置整体布局为votical
android:orientation="vertical"
5、在布局文件中添加新的线性布局,后续的控件将放入其中
<LinearLayout android:
layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout
6、在该布局中添加一张背景图片(home.png):先将图片放入drawable文件夹中,设置图片上外边距为100dp,水平居中
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/home"
android:layout_gravity="center"
android:layout_marginTop="100dp" />
7、添加两个文本输入框,用于输入账号和密码
文章来源:https://www.toymoban.com/news/detail-431225.html
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:layout_marginTop="20dp"
/>
<EditText
android:id="@+id/userpsd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
/>
8、添加两个复选按钮:记住密码和自动登录,其中记住密码为默认勾选(该控件为水平排布,因此新建线性布局,设置为水平排布)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="true"
android:text="记住密码"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="自动登录"
/>
</LinearLayout>
9、添加两个按钮:一个为登录,一个为注册
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/login"
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/login"
android:text="注册"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
10、创建另一个空白的Activity,名为RegisterActivity
11、在该活动中添加文本控件、密码控件、确认密码控件和一个注册按钮
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/username_reg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入用户名"
android:inputType="textPersonName"
/>
<EditText
android:id="@+id/password_reg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入密码"
android:inputType="textPassword"
/>
<EditText
android:id="@+id/password_confirm_reg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入密码"
android:inputType="textPassword"
/>
<Button
android:id="@+id/regiter_reg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:text="注册"
/>
</LinearLayout>
二、按键功能设置
1、设置登录按钮跳转到页面MainActivity中,显示HelloWorld
不同页面之间的跳转使用Intent对象,它可以用于在不同组件之间传递消息文章来源地址https://www.toymoban.com/news/detail-431225.html
1、首先在当前Activity中创建Intent对象,指向目标Activity,第一个参数是当前Activity的上下文,第二个参数是Activity的类
Intent intent = new Intent(this, TargetActivity.class);
2、 然后启动该intend
startActivity(intent);
public class LoginActivity extends AppCompatActivity {
private Button loginBtn,registerBtn;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
context=LoginActivity.this;
loginBtn=findViewById(R.id.login_btn);
registerBtn=findViewById(R.id.login_register);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(context,MainActivity.class);
startActivity(intent);
}
});
registerBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent();
intent.setClass(context,RegisterActivity.class);
startActivity(intent);
}
});
}
}
2、按键跳转处理使用以上方法外,还可以使用onclick事件,此时需要在button中添加该事件,同时在activity中声明该事件
<Button
android:id="@+id/login"
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="login"
/>
<Button
android:id="@+id/register"
android:text="注册"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="register"
/>
public void login(View view) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void register(View view) {
Intent intent = new Intent(this,RegisterActivity.class);
startActivity(intent);
}
到了这里,关于安卓学习1:简单的用户登录注册界面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!