Chương này giải thích, cách tạo màn hình đăng nhập và cách quản lý bảo mật khi thực hiện các lần thử sai.
Trước tiên, bạn phải xác định hai TextView hỏi tên người dùng và mật khẩu của người dùng. TextView mật khẩu phải có inputType được đặt thành mật khẩu. Cú pháp của nó được đưa ra dưới đây
<EditText android:id = "@+id/editText2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:inputType = "textPassword" /> <EditText android:id = "@+id/editText1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" />Xác định nút có văn bản đăng nhập và đặt thuộc tính onClick của nó . Sau đó xác định hàm được đề cập trong thuộc tính onClick trong tệp java.
<Button android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:onClick = "login" android:text = "@string/Login" />Trong tệp java, bên trong phương thức onClick nhận được tên người dùng và mật khẩu bằng cách sử dụng phương thức getText () và toString () và kết hợp nó với văn bản bằng hàm equals () .
EditText username = (EditText)findViewById(R.id.editText1); EditText password = (EditText)findViewById(R.id.editText2); public void login(View view){ if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){ //correcct password }else{ //wrong password }Điều cuối cùng bạn cần làm là cung cấp một cơ chế bảo mật để tránh những nỗ lực không mong muốn. Đối với điều này khởi tạo một biến và trên mỗi lần thử sai, giảm nó. Và khi nó đạt đến 0, vô hiệu hóa nút đăng nhập.
int counter = 3; counter--; if(counter==0){ //disble the button, close the application e.t.c }
Thí dụ
Đây là một ví dụ minh họa một ứng dụng đăng nhập. Nó tạo ra một ứng dụng cơ bản chỉ cung cấp cho bạn ba lần đăng nhập vào một ứng dụng.Để thử nghiệm với ví dụ này, bạn có thể chạy nó trên một thiết bị thực tế hoặc trong một trình giả lập.
| Các bước | Sự miêu tả |
|---|---|
| 1 | Bạn sẽ sử dụng Android studio để tạo ứng dụng Android theo gói com.example.sairamkrishna.myapplication. |
| 3 | Sửa đổi tệp src / MainActivity.java để thêm mã cần thiết. |
| 4 | Sửa đổi res / layout / activity_main để thêm các thành phần XML tương ứng |
| 5 | Chạy ứng dụng và chọn thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả |
Sau đây là nội dung của tệp hoạt động chính đã sửa đổi src / MainActivity.java .
package com.example.sairamkrishna.myapplication; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button b1,b2; EditText ed1,ed2; TextView tx1; int counter = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button)findViewById(R.id.button); ed1 = (EditText)findViewById(R.id.editText); ed2 = (EditText)findViewById(R.id.editText2); b2 = (Button)findViewById(R.id.button2); tx1 = (TextView)findViewById(R.id.textView3); tx1.setVisibility(View.GONE); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(ed1.getText().toString().equals("admin") && ed2.getText().toString().equals("admin")) { Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show(); tx1.setVisibility(View.VISIBLE); tx1.setBackgroundColor(Color.RED); counter--; tx1.setText(Integer.toString(counter)); if (counter == 0) { b1.setEnabled(false); } } } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }Sau đây là nội dung sửa đổi của xml res / layout / activity_main.xml .
Trong đoạn mã sau abc cho biết về logo của tutorialspoint.com
<?xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height = "match_parent" android:paddingLeft= "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" android:paddingBottom = "@dimen/activity_vertical_margin" tools:context = ".MainActivity"> <TextView android:text = "Login" android:layout_width="wrap_content" android:layout_height = "wrap_content" android:id = "@+id/textview" android:textSize = "35dp" android:layout_alignParentTop = "true" android:layout_centerHorizontal = "true" /> <TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Tutorials point" android:id = "@+id/textView" android:layout_below = "@+id/textview" android:layout_centerHorizontal = "true" android:textColor = "#ff7aff24" android:textSize = "35dp" /> <EditText android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id = "@+id/editText" android:hint = "Enter Name" android:focusable = "true" android:textColorHighlight = "#ff7eff15" android:textColorHint = "#ffff25e6" android:layout_marginTop = "46dp" android:layout_below = "@+id/imageView" android:layout_alignParentLeft = "true" android:layout_alignParentStart = "true" android:layout_alignParentRight = "true" android:layout_alignParentEnd = "true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:id="@+id/editText2" android:layout_below="@+id/editText" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignRight="@+id/editText" android:layout_alignEnd="@+id/editText" android:textColorHint="#ffff299f" android:hint="Password" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Attempts Left:" android:id="@+id/textView2" android:layout_below="@+id/editText2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:textSize="25dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView3" android:layout_alignTop="@+id/textView2" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignBottom="@+id/textView2" android:layout_toEndOf="@+id/textview" android:textSize="25dp" android:layout_toRightOf="@+id/textview" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="login" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_toLeftOf="@+id/textview" android:layout_toStartOf="@+id/textview" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" android:id="@+id/button2" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/textview" android:layout_toEndOf="@+id/textview" /> </RelativeLayout>Sau đây là nội dung của res / values / string.xml .
<resources> <string name="app_name">My Application</string> </resources>Sau đây là nội dung của tệp AndroidManifest.xml .
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>Hãy thử chạy ứng dụng của chúng tôi, chúng tôi vừa sửa đổi. Tôi cho rằng bạn đã tạo AVD của mình trong khi thiết lập môi trường.
Để chạy ứng dụng từ Android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ.
Android studio cài đặt ứng dụng trên AVD của bạn và khởi động ứng dụng và nếu mọi thứ đều ổn với thiết lập và ứng dụng của bạn, ứng dụng sẽ hiển thị cửa sổ Trình mô phỏng sau
![]() |
| Học lập trình Android |
Nhập bất kỳ thứ gì vào trường tên người dùng và mật khẩu, sau đó nhấn nút đăng nhập. Tôi đặt abc vào trường tên người dùng và abc trong trường mật khẩu. Tôi đã thất bại. Điều này được hiển thị bên dưới
Làm điều này hai lần nữa, và bạn sẽ thấy rằng bạn có 0 lần đăng nhập còn lại và nút đăng nhập của bạn bị vô hiệu hóa.
Bây giờ hãy mở lại ứng dụng và lần này nhập tên người dùng đúng là quản trị viên và mật khẩu làm quản trị viên và nhấp vào đăng nhập. Bạn sẽ đăng nhập thành công.
Nếu người dùng nhấn vào nút hủy, nó sẽ đóng một ứng dụng của màn hình đăng nhập.

Không có nhận xét nào:
Đăng nhận xét