2017年6月3日 星期六

Android :登入程式(Sharedpreferences+AlertDialog+Notification)範例

程式情境描述:MainActivity1可以讓使用者登入和註冊,如果註冊過了就會出現警告;MainActivity2是使用者的註冊畫面;MainActivity3登入後的畫面,用來顯示資訊和會發出通知(Notification)。

一般登入和註冊的部分不會使用sharedpreferences,是為了練習方便才使用的。

程式如下:

1.MainActivity1登入和註冊程式:




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:padding="25dp"
    tools:context="tw.com.hjchen.loginhw.MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="帳號 : " />
        <EditText
            android:id="@+id/id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密碼 : " />
        <EditText
            android:id="@+id/pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <Button
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="登入"
            android:onClick="login"/>
        <Button
            android:id="@+id/cancel"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="註冊"
            android:onClick="register"/>
    </LinearLayout>
    
</LinearLayout>


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private Button cancel;
    private EditText id,pwd;
    private String loginid,loginpwd;
    private  Boolean loggedin = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        id = (EditText)findViewById(R.id.id);
        pwd = (EditText)findViewById(R.id.pwd);
        cancel = (Button)findViewById(R.id.cancel);
        SharedPreferences result = getSharedPreferences("personal",0);
        loggedin = result.getBoolean("loggedin",false);
        if (loggedin){
            Intent it = new Intent(MainActivity.this,Main3Activity.class);
            startActivity(it);
        }
    }
    public void login(View view){
        SharedPreferences result = getSharedPreferences("personal",0);
        loginid = result.getString("uid","無");
        loginpwd = result.getString("upwd","無");
        if (loginid.equals(id.getText().toString())){
            if (loginpwd.equals(pwd.getText().toString())){
                result.edit().putBoolean("loggedin",true).commit();
                Intent it = new Intent(MainActivity.this,Main3Activity.class);
                startActivity(it);
            }
        }


    }
    public void register(View view){
        SharedPreferences result = getSharedPreferences("personal",0);

        if (result.contains("uid")){
            if (result.contains("upwd")) {
                AlertDialog.Builder d = new AlertDialog.Builder(MainActivity.this);
                d.setTitle("警告!")
                        .setMessage("已註冊")
                        .setCancelable(false);
                d.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                d.show();
            }
        }else {
            Intent it = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(it);
        }
    }
}


2.MainActivity2註冊程式碼:

                   
                                                                          註冊畫面
  
如果已註冊,會出現警告視窗




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:padding="25dp"
    tools:context="tw.com.hjchen.loginhw.Main2Activity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="帳號 : " />
        <EditText
            android:id="@+id/id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密碼 : " />
        <EditText
            android:id="@+id/pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性別 : "/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名 : " />
        <EditText
            android:id="@+id/edt03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <Button
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="確定"
            android:onClick="registerok"/>
        <Button
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="取消"
            android:onClick="cancel"/>
    </LinearLayout>

</LinearLayout>


import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Main2Activity extends AppCompatActivity {

    private EditText id,pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        id = (EditText)findViewById(R.id.id);
        pwd = (EditText)findViewById(R.id.pwd);
    }
    public void registerok(View view){
        SharedPreferences result = getSharedPreferences("personal",0);
        String userid = id.getText().toString();
        String userpwd = pwd.getText().toString();
        result.edit()
                .putString("uid",userid)
                .putString("upwd",userpwd)
                .commit();
        finish();

    }
    public void cancel(View view){
        finish();
    }
}

3.MainActivity3顯示資訊和發送通知程式碼:





<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="tw.com.hjchen.loginhw.Main3Activity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_weight="3">
        <TextView
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="歡迎光臨"
            android:layout_gravity="center"
            android:textSize="35sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <ListView
            android:id="@+id/listView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/info"
            android:layout_marginTop="30dp"></ListView>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom|center"
        android:layout_weight="3">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登出"
            android:onClick="logout" />
    </LinearLayout>
    
</LinearLayout>


import android.app.Notification;
import android.app.NotificationManager;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main3Activity extends AppCompatActivity {

    private int i=0;
    private static final String[] mStrings={"海景房","山景房","海砂屋","帳篷","組合屋","鐵皮屋","打地鋪","小木屋"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        Notify();
        ListView list = (ListView)findViewById(R.id.listView1);
        list.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,mStrings));
        list.setTextFilterEnabled(true);
    }
    public void Notify(){
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.house01)
                .setTicker("颱風特價")
                .setContentTitle("颱風限定")
                .setContentText("海砂屋任你住");
        Notification notification = builder.build();
        notificationManager.cancel(i);                 //移除ID值為0的通知
        notificationManager.notify(i,notification);    //發送ID值為0的通知
    }
    public void logout(View view){
        SharedPreferences result = getSharedPreferences("personal",0);
        result.edit().putBoolean("loggedin",false).commit();
        finish();
    }
}


1 則留言:

  1. 你好 可以請教一下 public void login(View view){
    SharedPreferences result = getSharedPreferences("personal",0);
    loginid = result.getString("uid","無");
    loginpwd = result.getString("upwd","無");
    if (loginid.equals(id.getText().toString())){
    if (loginpwd.equals(pwd.getText().toString())){
    result.edit().putBoolean("loggedin",true).commit();
    Intent it = new Intent(MainActivity.this,Main3Activity.class);
    startActivity(it);

    這段的動作是甚麼意思嗎

    回覆刪除