1. Buat class Login.java
package thefly.org;
import go_antri.org.R;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends Activity {
EditText username,password;
TextView status;
Button login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
username=(EditText) findViewById(R.id.txtusername);
password=(EditText) findViewById(R.id.txtpassword);
status=(TextView) findViewById (R.id.txtstatus);
login=(Button) findViewById (R.id.btn_login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
//"user" itu adalah nama fild dari database yang di tuju
postParameters.add(new BasicNameValuePair("username", username.getText().toString()));
postParameters.add(new BasicNameValuePair("password", password.getText().toString()));
String response = null;
try {
response = CustomHttpClient.executeHttpPost(CustomHttpClient.URL+"check_login.php", postParameters);
String res = response.toString();
res = res.trim();
res = res.replaceAll("\\s+","");
status.setText(res);
if (res.equals("1"))
{
status.setText("Berhasil Login");
btn_login_Clicked(v);
}
else {
status.setText("Maaf!! Username dan Password tidak sesuai");
}
}
catch (Exception e) {
status.setText(e.toString());
}
//btn_login_Clicked(v);
}
});
}
// melakukan pendaftaran user
public void btn_daftar_Clicked(View v) {
Intent panggil_class = new Intent(this, pendaftaran_user.class);
startActivity(panggil_class);
}
// apabila user berhasil login.
public void btn_login_Clicked(View v) {
Intent panggil_class = new Intent(this, list_dokter.class);
startActivity(panggil_class);
}
public void keluar(){
AlertDialog.Builder pesan= new AlertDialog.Builder(this);
pesan.setMessage("Anda Yakin Ingin Menutup Aplikasi SISTEM ANATA SALON N SKINCARE?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_BACK){
keluar();
}
return super.onKeyDown(keyCode, event);
}
}
3. Buat login.xml di folder layout untuk tampilan.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ec5f98">
<ImageView
android:id="@+id/imageView1"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:adjustViewBounds="true"
/>
<!-- Header Starts-->
<LinearLayout android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<!-- Logo Start-->
<ImageView android:src="@drawable/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="@layout/footer_repeat"
android:layout_alignParentBottom="true">
</LinearLayout>
<!-- Footer Ends -->
<!-- Login Form -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="@id/header">
<!-- Email Label -->
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/l" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:id="@+id/tableLayout">
<TableRow android:padding="10dp"
android:background="#eae47e">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username : "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<EditText
android:id="@+id/txtusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:inputType="none" />
</TableRow>
<TableRow android:layout_marginTop="10dp"
android:padding="10dp"
android:background="#eae47e">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password :"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<EditText
android:id="@+id/txtpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:padding="5dp"
android:layout_marginLeft="10dp"
android:layout_weight="1" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/txtstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#b00808"
android:text=""
android:textStyle="bold"
android:textSize="15sp"
android:layout_marginBottom="10dp" />
<Button
android:id="@+id/btn_login"
style="@style/btnStyleSeagull"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="22dp"
android:background="#e01080"
android:onClick="btn_login_Clicked"
android:text="LOGIN"
android:textColor="#ffffff"
android:textStyle="bold" />
<Button
android:id="@+id/link_daftar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_login"
android:layout_below="@+id/btn_login"
android:layout_marginTop="18dp"
android:onClick="btn_daftar_Clicked"
android:text="Belum punya akun? Register disini"
android:textColor="#000000"/>
</LinearLayout>
<!-- Login Form Ends -->
</RelativeLayout>
</ScrollView>
0 komentar:
Posting Komentar