android开发

利用 Android Studio制作一个美观的界面

  1. 第一步:创建一个Android项目login_text
  2. 第二步:修改activity_main.xml里面的代码(增加如下控件,特别注意图片放在drawable文件里面)
    代码如下:
<?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:background="#E6E6E6">
    <ImageView
        android:id="@+id/iv"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        app:srcCompat="@drawable/head" />

    <LinearLayout
        android:id="@+id/ll_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="5dp"
        android:background="#FFFFFF"
         >

        <TextView
            android:id="@+id/tv_number"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="账号"
            android:textColor="#000000"
            android:textSize="20dp" />

        <EditText
            android:id="@+id/dt_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp"
              />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ll_number"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="5dp"
        android:background="#FFFFFF">

        <TextView
            android:id="@+id/tv_password"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密码"
            android:textColor="#000000"
            android:textSize="20dp" />

        <EditText
            android:id="@+id/dt_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:inputType="textPassword"
            android:padding="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/mm_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/ll_password"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:background="@null"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/remember_pass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="保存密码"
            android:textColor="#000000"
            android:textSize="20dp"
            >

        </CheckBox>
    </LinearLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mm_password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#3C8DC4"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:text="登录" />

</RelativeLayout>
  1. 第三步:修改MainActivity里面的代码:
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn_login;
    private EditText accountEdit;
    private EditText passwordEdit;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private CheckBox rememberPass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref= PreferenceManager.getDefaultSharedPreferences(this);
        rememberPass=(CheckBox)findViewById(R.id.remember_pass);
        btn_login=(Button)findViewById(R.id.btn_login);
        accountEdit=(EditText) findViewById(R.id.dt_number);
        passwordEdit=(EditText) findViewById(R.id.dt_password);
        boolean isRemember=pref.getBoolean("remember_password",false);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account =accountEdit.getText().toString();
                String password=passwordEdit.getText().toString();

                if(!account.equals("")&&!password.equals(""))
                {
                    Intent intent =new Intent(MainActivity.this,OpenedActivity.class);
                    intent.putExtra("username",account);
                    intent.putExtra("password",password);

                    startActivity(intent);
                   finish();
                }else{
                    Toast.makeText(MainActivity.this,"please input account and password",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}

4.运行结果如下图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值