1. 程式人生 > >製作一個安卓介面,可以進行密碼和賬號的判斷,有用來儲存輸入賬號和密碼的類,用來在其他類中呼叫

製作一個安卓介面,可以進行密碼和賬號的判斷,有用來儲存輸入賬號和密碼的類,用來在其他類中呼叫

首先附上安卓介面圖和完整版下載地址:

下載地址:https://download.csdn.net/download/qq_39343904/10881646

                              

 

同時可以在使用者輸入錯誤的賬戶或者密碼時彈窗提醒。其中內建了類可以用來儲存輸入的賬戶或密碼,以便於其他類呼叫時使用。

首先需要在AndroidManifest.xml裡新增這樣一句話,將上面的框子去掉。

android:theme="@android:style/Theme.Light.NoTitleBar"

當然也可以直接點開AndroidManifest.xml,在下方選擇Application,然後在裡面選擇主題。

接下來是佈局檔案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/anzhuo"
     android:gravity="top"
    android:orientation="vertical" 
    tools:context="com.example.face_a.MainActivity" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" >

        <Button
            android:id="@+id/forget_passwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="17dp"
            android:background="#0000"
            android:onClick="login_pw"
            android:text="忘記密碼?"
            android:textColor="@android:color/white"
            android:textSize="14sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/login_div"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_denglu"
        android:layout_centerHorizontal="true"
        android:layout_margin="15dip"
        android:background="@drawable/background_login_div_bg"
        android:gravity="center_horizontal"
        android:padding="15dip" >

        <TextView
            android:id="@+id/login_user_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="5dp"
            android:text="賬戶"
            android:textColor="@android:color/white" />

        <EditText
            android:id="@+id/username_edit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@id/login_user_input"
            android:background="@android:color/transparent"
            android:hint="請輸入賬戶"
            android:inputType="text"
            android:singleLine="true"
            android:textColor="@android:color/white" />

        <TextView
            android:id="@+id/login_password_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/username_edit"
            android:layout_marginTop="3dp"
            android:text="密碼" 
            android:textColor="@android:color/white"/>

        <EditText
            android:id="@+id/password_edit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/login_password_input"
            android:layout_below="@id/login_password_input"
            android:background="@android:color/transparent"
            android:hint="請輸入密碼"
            android:inputType="textPassword"
            android:password="true"
            android:singleLine="true" />
    </RelativeLayout>

    <Button
        android:id="@+id/btn_denglu"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_above="@+id/relativeLayout1"
        android:layout_alignLeft="@+id/login_div"
        android:layout_alignRight="@+id/login_div"
        android:background="@drawable/blue_button"
        android:text="登陸"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp"
        android:background="#0000"
        android:onClick="user"
        android:text="使用者註冊|無法登陸"
        android:textColor="@android:color/white"
        android:textSize="14sp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#0000"
        android:onClick="login_pw"
        android:text="XX軟體"
        android:textColor="@android:color/white"
        android:textSize="22sp" />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Button01"
        android:background="#696969" />

</RelativeLayout>

佈局檔案寫好後,就要將往上新增功能了:

定義一些變數:

Button btn_denglu;
private EditText username = null; // 帳號編輯框
private EditText password; // 密碼編輯框
private Button button = null;

傳入佈局檔案的控制元件:

username= (EditText)findViewById(R.id.username_edit);
password = (EditText)findViewById(R.id.password_edit);
btn_denglu=(Button) findViewById(R.id.btn_denglu);

對於點選事件的程式碼如下:

public void onClick(View v) {
		if(/*"zty".equals(username.getText().toString()) && */"666666".equals(password.getText().toString()))   //判斷 帳號和密碼
		{
		String result = username.getText().toString();
		WirelessApp app = (WirelessApp)getApplicationContext();
		app.setUsername(result);
		Intent intent = new Intent();
		/* intent.setClass(Denglu.this,MainActivity.class);
		startActivity(intent);
		finish();*/ 
		Intent intent1 = new Intent(MainActivity.this, MainActivity.class);
		startActivity(intent1);
		//Toast.makeText(getApplicationContext(), "登入成功", Toast.LENGTH_SHORT).show();
			          }
		else if("".equals(username.getText().toString()) || "".equals(password.getText().toString()))   //判斷 帳號和密碼
		{
		new AlertDialog.Builder(MainActivity.this)
		.setIcon(getResources().getDrawable(R.drawable.login_error_icon))
		.setTitle("登入錯誤")
		.setMessage("帳號或者密碼不能為空,\n請輸入後再登入!")
		.create().show();
		}
		else{
			              
		new AlertDialog.Builder(MainActivity.this)
		.setIcon(getResources().getDrawable(R.drawable.login_error_icon))
		.setTitle("登入失敗")
		.setMessage("帳號或者密碼不正確,\n請檢查後重新輸入!")
		.create().show();
		}
					
		/*Intent intent=new Intent(Denglu.this ,MainActivity.class);
		startActivity(intent);*/
				}

		private void finish() {
					// TODO 自動生成的方法存根		
				}
		
			});			

注意到上面定義了一個類叫做WirelessApp,這個類是用來儲存使用者輸入的賬號的,以便於在其他類中呼叫。

WirelessApp中的程式碼如下:

public class WirelessApp extends Application {
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}

這樣一個安卓APP的簡單登陸基本就製作完成了,還注意到在登陸介面上有使用者註冊和忘記密碼,我們就在上面新增兩個uri,這樣可以在使用者點選時候跳轉到相應的網頁上去。

public void login_pw(View v) {     
    	Uri uri = Uri.parse("https://blog.csdn.net/qq_39343904/article/details/85111956"); 
    	Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
    	startActivity(intent);
      }  
	
public void user(View v) {     
    	Uri uri = Uri.parse("https://blog.csdn.net/qq_39343904/article/details/85111956"); 
    	Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
    	startActivity(intent);
      }  

當然這只是登陸介面,在具體使用的時候,輸入賬戶和密碼正確,在需要跳轉介面時會閃退,原因是沒有新增跳轉的介面,這就需要您加入需要跳轉到的介面了。

如果哪裡有紕漏疏忽錯誤等,歡迎各位前輩批評指正。