Rabu, 05 Oktober 2016

Android Login dari .txt


<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:background="@drawable/bg2"
    tools:context=".Loading" >

    <EditText
        android:id="@+id/edPass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="30dp"
        android:layout_toRightOf="@+id/btnLogin"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnLogin"
        android:layout_alignBottom="@+id/btnLogin"
        android:layout_alignRight="@+id/edPass"
        android:layout_marginRight="23dp"
        android:text="Cancel" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/edPass"
        android:layout_marginLeft="53dp"
        android:layout_marginTop="31dp"
        android:text="Login" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:text="AUTHENTICATION"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/btnLogin"
        android:layout_centerVertical="true"
        android:text="Password"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

</RelativeLayout>

-----------------------------------------------------------

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.KeyEvent;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity {
EditText edPass;
private static final String TAG = Login.class.getName();
private static final String FILENAME = "myFiles.txt";

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

  String textFromFileString =  "";

       try{
       textFromFileString=readFromFile();
       if(textFromFileString.length()<1){
        String passDefault = "admin";
       writeToFile(passDefault);
     
        }
     
       }catch(Exception ee){}
     
     
edPass=(EditText)findViewById(R.id.edPass);
Button btnLogin=(Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String pass=edPass.getText().toString();
if(pass.length()<1){
lengkapi();
}
else{
String textToSaveString=readFromFile();
 if ( textToSaveString.equalsIgnoreCase(pass) )
           sukses();
       else
        gagal();
}
}});


Button btnCancel=(Button)findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
keluarYN();
}});
     
   
     
     
         }
   
   private void writeToFile(String data) {
       try {
           OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
           outputStreamWriter.write(data);
           outputStreamWriter.close();
       }
       catch (IOException e) {
           Log.e(TAG, "File write failed: " + e.toString());
       }
       
   }

   private String readFromFile() {
       String ret = "";
       try {
           InputStream inputStream = openFileInput(FILENAME);
           if ( inputStream != null ) {
               InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
               BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
               String receiveString = "";
               StringBuilder stringBuilder = new StringBuilder();
               
               while ( (receiveString = bufferedReader.readLine()) != null ) {
                   stringBuilder.append(receiveString);
               }
               
               inputStream.close();
               ret = stringBuilder.toString();
           }
       }
       catch (FileNotFoundException e) {
           Log.e(TAG, "File not found: " + e.toString());
       } catch (IOException e) {
           Log.e(TAG, "Can not read file: " + e.toString());
       }

       return ret;
   }
public void keluar(){
new AlertDialog.Builder(this)
.setTitle("Menutup Aplikasi")
.setMessage("Terimakasih... Anda Telah Menggunakan Aplikasi Ini")
.setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
finish();
}})
.show();
}

public void sukses(){
new AlertDialog.Builder(this)
.setTitle("Sukses Login")
.setMessage("Login Berhasil....")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
edPass.setText("");
Intent i = new Intent(Login.this, Menu_Utama.class);
        startActivity(i);
}})
.show();
}

public void gagal(){
new AlertDialog.Builder(this)
.setTitle("Login Gagal")
.setMessage("Maaf, Login Anda Gagal..silakan cek kembali")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {

}})
.show();
}

public void lengkapi(){
new AlertDialog.Builder(this)
.setTitle("Login Gagal")
.setMessage("Maaf, silakan isi data password Anda")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {

}})
.show();
}

public void keluarYN(){
AlertDialog.Builder ad=new AlertDialog.Builder(Login.this);
ad.setTitle("Konfirmasi");
ad.setMessage("Apakah benar ingin keluar?");
ad.setPositiveButton("Yes",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}});
           
ad.setNegativeButton("No",new OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
}});
ad.show();
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
keluarYN();
return true;
}
return super.onKeyDown(keyCode, event);
}  

}

Tidak ada komentar:

Posting Komentar