Android開發(fā)之簡單登錄界面

用戶界面基礎(chǔ)

  • Android系統(tǒng)的四大組件分別是活動(Activity)、服務(wù)(Service)、廣播接收器(Broadcast Receiver)、內(nèi)容提供器(Content Provider)。
    其中,活動算是一個程序的門面,活動通過onCreate()方法來對用戶界面(UI)進(jìn)行初始化。而用戶界面的創(chuàng)建則分為靜態(tài)和動態(tài)兩種方式:
  1. 靜態(tài)方式即以XML布局文件來定義用戶界面,通過XML布局文件中的相關(guān)屬性進(jìn)行控制,我將使用這種方式來實(shí)現(xiàn)簡單的登錄界面。
  2. 動態(tài)方式是指通過Java代碼來開發(fā)用戶界面,動態(tài)地控制界面中的組件。我沒有嘗試過這種開發(fā)方式。
  • Android項(xiàng)目工程的結(jié)構(gòu):


    Android項(xiàng)目工程結(jié)構(gòu)
  • 補(bǔ)充:

  1. 其中java文件夾下存儲有主活動文件MainActivty.java。
  2. layout文件夾下存儲有布局文件activity_main.xml。

簡單登錄界面的實(shí)現(xiàn)

  • 準(zhǔn)備完成的功能:
  1. 界面包含兩個編輯框,一個用于輸入用戶名;一個用于輸入密碼。
  2. 點(diǎn)擊登錄按鈕,若用戶名和密碼均和點(diǎn)擊登錄按鈕方法中定義的相同,則顯示登錄成功的提示框,若不同,則提示登錄失敗。
  • 按照要求,實(shí)現(xiàn)兩個輸入框,這里我們就需要在XML文件中進(jìn)行布局,使用EditText控件進(jìn)行設(shè)置。
    XML布局代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <TextView
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用戶登錄界面"
        android:textAlignment="center"
        android:textSize="24sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用戶名"
        android:textAlignment="center"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入您的用戶名"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="密碼"
        android:textAlignment="center"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入您的密碼"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <Button
        android:id="@+id/login"
        android:layout_height="60dp"
        android:layout_width="wrap_content"
        android:text="登錄"
        android:layout_gravity="center"
        android:textAlignment="center"
        android:textSize="18sp" />

</LinearLayout>


</LinearLayout>
  • 效果如下:


    登錄界面
  • 補(bǔ)充:
  1. LinearLayout代表當(dāng)前布局方式為線性布局。
  2. TextView為顯示字符串的控件,例如界面上的“用戶登錄界面”、“用戶名”、“密碼”就是用TextView實(shí)現(xiàn)的。
  3. Button為按鈕控件,即為用戶界面添加一個可供點(diǎn)擊的按鈕,并可在主活動中添加相應(yīng)的方法實(shí)現(xiàn)點(diǎn)擊按鈕后要進(jìn)行的操作。
  • 接下來我們開始編寫活動頁面的代碼,完成點(diǎn)擊按鈕后的匹配用戶名和密碼的操作。
  1. 為獲取到的Button對象綁定一個監(jiān)聽器button.setOnClickListener()。
  2. 使用接口方式實(shí)現(xiàn)監(jiān)聽事件。
  3. 最后在onClick()方法中實(shí)現(xiàn)監(jiān)聽事件要實(shí)現(xiàn)的邏輯即可。
    代碼如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button button;
    EditText username;
    EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.login);
        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String username1 = username.getText().toString();
        String password1 = password.getText().toString();
        String ok = "登錄成功";
        String fail = "登錄失敗";
        if (username1.equals("lhk") && password1.equals("123456")) {
            Toast.makeText(MainActivity.this,ok,Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(MainActivity.this,fail,Toast.LENGTH_SHORT).show();
        }

    }
}
  • 登陸成功效果如下:


    登陸成功
  • 登錄失敗效果如下:


    登錄失敗

Github地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容