1、创建login.xml文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:stretchColumns="0,3"
>
<!-- 第一行 -->
<TableRow android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView/>
<TextView android:text="帐 号:"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:textSize="24px"
android:layout_height="wrap_content"
/>
<EditText android:id="@+id/editText1"
android:textSize="24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="200px"/>
<TextView />
</TableRow>
<!-- 第二行 -->
<TableRow android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView/>
<TextView android:text="密 码:"
android:id="@+id/textView2"
android:textSize="24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="24px"
android:id="@+id/editText2"
android:inputType="textPassword"/>
<TextView />
</TableRow>
<!-- 第3行 -->
<TableRow android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView/>
<Button android:text="登录"
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:text="退出"
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView />
</TableRow>
</TableLayout>
2、创建Data类
public final class Data {
//用户信息
public static String[][] USER = {
{"1001","111","Jaer"},
{"1002","111","Soft"},
{"1003","111","Lol"},
};
}
3、创建一个继承android.app.Activity的LoginAcitivity,java
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);//设置该Activity
Button button = (Button)findViewById(R.id.login);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String number = ((EditText)findViewById(R.id.editText1)).getText().toString();
String pwd = ((EditText)findViewById(R.id.editText2)).getText().toString();
boolean flag = false;//用于记录登录是否成功的标记变量
String nickname = "";//保存昵称
//通过遍历数据的形式判断输入的账号和密码是否正确
for (int i = 0; i < Data.USER.length; i++) {
if(number.equals(Data.USER[i][0])){//判断账号是否正确
if(pwd.equals(Data.USER[i][1])){//判断密码是否正确
nickname = Data.USER[i][2];//获得昵称
flag = true;//将标志变量设置为true
break;//跳出for循环
}
}
}
if(flag){
//创建要显示 Activity对应的Intent对象
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
Bundle bundle =new Bundle();//创建一个Bundle的对象的bundle
bundle.putString("nickname", nickname);//保存昵称
intent.putExtras(bundle);//将数据包添加到Intent对象中
startActivity(intent);//开启一个新的Activity
}else{
Toast.makeText(LoginActivity.this, "您输入的账号或密码错误!", Toast.LENGTH_SHORT).show();
}
}
});
Button exit = (Button)findViewById(R.id.exit);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();//关闭当前Activity
}
});
}
}
4、activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/nickname"
android:layout_width="wrap_content"
android:layout_weight="9"
android:textSize="24px"
android:padding="20px"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/m_exit"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出登录" />
</LinearLayout>
<ListView
android:id="@+id/listView1"
android:entries="@array/option"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
5、在res\value目录中创建一个定义数组资源的xml文件arrays.xml,并在该文件中添加名称为option的字符串数组
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="option">
<item >在线好友</item>
<item >我的好友</item>
<item >陌生人</item>
<item >黑名单</item>
</string-array>
</resources>
6、在MainActivity的onCreate()方法中
Intent intent = getIntent();//获取Intent对象
Bundle bundle = intent.getExtras();//获取传递的数据包
String nickname = bundle.getString("nickname");//获取传递过来的昵称
TextView tv = (TextView)findViewById(R.id.nickname);//获取用于显示当前的登录用户的TextView组件
tv.setText("当前登录:"+nickname);//显示当前登录用户的昵称
Button button = (Button)findViewById(R.id.m_exit);//获取“退出登录”按钮
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();//关闭当前Activity
}
});
7、AndroidManifest.xml文件,修改入口Activity
<activity
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog"
android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.android5_8.MainActivity"
android:label="主界面" >
</activity>