Vous êtes sur la page 1sur 4

Learning Native Droid - Part I (Activity & Layout)

Case 1 : Form Login


Layout : ActivityMain.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">

<TableRow>
<TextView
android:text="User Name: "
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>

<EditText
android:text=""
android:id="@+id/txtUname"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</TableRow>

<TableRow>
<TextView
android:text="Password: "
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>

<EditText
android:text=""
android:id="@+id/txtPwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true">
</EditText>
</TableRow>

<TableRow>
<Button
android:text="Cancel"
android:id="@+id/btnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>

<Button
android:text="Login"
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>

</TableRow>
</TableLayout>
Activity : MainActivity.java
package com.example.hello_world;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

EditText txtUserName, txtPassword;


String UserName="dian", Password="yolo";
Button btnLogin, btnCancel;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private void initView(){


txtUserName=(EditText)this.findViewById(R.id.txtUname);
txtPassword=(EditText)this.findViewById(R.id.txtPwd);
btnLogin=(Button)this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

if((txtUserName.getText().toString()).equals(UserName)){

if((txtPassword.getText().toString()).equals(Password)){
Toast.makeText(MainActivity.this,
"Login Success",Toast.LENGTH_LONG).show(); }
else
Toast.makeText(MainActivity.this,
"Invalid Login - Wrong Password",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "Invalid Login
- Wrong Username",Toast.LENGTH_LONG).show();
}
}
});
btnLogout=(Button)this.findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.activity_main);
}
});
}

Output

Case 2 : Form Setelah Login


Layout : Bonjour.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">

<TableRow>
<TextView
android:text="Selamat Datang "
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>

<TextView
android:text=""
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</TableRow>

<TableRow>
<Button
android:text="Log Out"
android:id="@+id/btnLogout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</TableRow>
</TableLayout>

Output

Vous aimerez peut-être aussi