package com.gcntc.news_editing_offline.ui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.gcntc.news_editing_offline.R;
public class Login extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String user=((EditText)findViewById(R.id.editText1)).getText().toString();
String pwd=((EditText)findViewById(R.id.editText2)).getText().toString();
user=pwd="admin";
boolean flag=false;
for(int i=0;i<Data.USER.length;i++){
if(user.equals(Data.USER[i][0])){
if(pwd.equals(Data.USER[i][1])){
flag=true;
break;
}
}
}
if(flag){
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
}else{
Toast.makeText(Login.this, "用户名或密码错误!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/welcome_bg" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@+id/editText1"
android:layout_marginBottom="20dp"
android:src="@drawable/welcome_app_icon" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerHorizontal="true"
android:layout_above="@+id/editText2"
android:layout_marginTop="5dp"
android:ems="10"
android:textColorHint="@color/gray"
android:hint="用户名"
android:text="admin">
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerInParent="true"
android:ems="10"
android:hint="密码"
android:textColorHint="@color/gray"
android:inputType="textPassword"
android:text="admin"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@id/editText2"
android:layout_alignRight="@id/editText2"
android:layout_marginTop="5dp"
android:background="@drawable/btn_login"
android:text="登录"
android:textColor="@color/white"
/>
</RelativeLayout>
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.gcntc.news_editing_offline.R;
public class Login extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String user=((EditText)findViewById(R.id.editText1)).getText().toString();
String pwd=((EditText)findViewById(R.id.editText2)).getText().toString();
user=pwd="admin";
boolean flag=false;
for(int i=0;i<Data.USER.length;i++){
if(user.equals(Data.USER[i][0])){
if(pwd.equals(Data.USER[i][1])){
flag=true;
break;
}
}
}
if(flag){
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
}else{
Toast.makeText(Login.this, "用户名或密码错误!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/welcome_bg" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@+id/editText1"
android:layout_marginBottom="20dp"
android:src="@drawable/welcome_app_icon" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerHorizontal="true"
android:layout_above="@+id/editText2"
android:layout_marginTop="5dp"
android:ems="10"
android:textColorHint="@color/gray"
android:hint="用户名"
android:text="admin">
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerInParent="true"
android:ems="10"
android:hint="密码"
android:textColorHint="@color/gray"
android:inputType="textPassword"
android:text="admin"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@id/editText2"
android:layout_alignRight="@id/editText2"
android:layout_marginTop="5dp"
android:background="@drawable/btn_login"
android:text="登录"
android:textColor="@color/white"
/>
</RelativeLayout>