android 登陆界面
LoginActivity.java
package com.example.ruian; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class LoginActivity extends Activity { private EditText m_txtUserName = null; private EditText m_txtPwd = null; private Button m_butLogin=null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); findViews(); setListensers(); } private void findViews(){ m_txtUserName=(EditText) findViewById(R.id.txtUserName); m_txtPwd=(EditText) findViewById(R.id.txtPwd); m_butLogin = (Button) findViewById(R.id.butLogin); } private void setListensers(){ //Login按钮 m_butLogin.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { butLogin_OnClick(v); } }); } private void butLogin_OnClick(View v){ //系统参数 SharedPreferences share = getSharedPreferences("perference",MODE_PRIVATE); //登陆密码--------------------------------------------------------------- String strLoginPwd_value=share.getString("login_pwd_value",""); //m_txtPwd.setText(strLoginPwd_value); if(strLoginPwd_value.equals("")==true){//为空则设初始值 strLoginPwd_value=getResources().getString(R.string.loginpwd_init);//默认登陆密码 } String strLoginPwd=m_txtPwd.getText().toString(); if(strLoginPwd.equals(strLoginPwd_value)==true){ //登陆成功 //声明Intent对象,并启动 LoginActivity Activity Intent intent = new Intent(); intent.setClass(LoginActivity.this, MainActivity.class); startActivity(intent); //从历史堆栈中删除当前Activity,用户点击“返回”按钮键时,不会再返回到这个Activity。 finish(); } else{ openDialog("登陆密码错误!","消息"); } } //弹出对话框-------------------------------------------------- private void openDialog(String strMsg, String strTitle){ new AlertDialog.Builder(this) .setTitle(strTitle) .setMessage(strMsg) .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .show(); } //弹出确认、取消 对话框-------------------------------------------------- private void openDialogOk(String strMsg, String strTitle){ new AlertDialog.Builder(this) .setTitle(strTitle) .setMessage(strMsg) .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub openDialog("登陆密码错误!-----","消息"); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub openDialog("登陆密码错误!=====","消息"); } }) .show(); } }
activity_login.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="20dip" android:layout_weight="1" android:text="用户登陆" /> </TableRow> <TableRow> <TextView android:id="@+id/lblUserName" android:layout_weight="1" android:text="用户名:" /> </TableRow> <TableRow> <EditText android:id="@+id/txtUserName" android:layout_weight="1" android:inputType="textPassword"> </EditText> </TableRow> <TableRow> <TextView android:id="@+id/lblPwd" android:layout_weight="1" android:text="密码:" /> </TableRow> <TableRow> <EditText android:id="@+id/txtPwd" android:layout_weight="1" android:inputType="textPassword"> </EditText> </TableRow> <TableRow> <Button android:id="@+id/butLogin" android:layout_weight="1" android:text="登陆" /> </TableRow> </TableLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="loginpwd_init">123456</string> </resources>
最重要的一步,将此界面设置为启动页。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ruian" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.ruian.LoginActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="ParaSetActivity"></activity> <activity android:name="MainActivity"></activity> </application> </manifest>
说明:
android:name="com.example.ruian.LoginActivity" 这句中的 LoginActivity 即是设置初始界面为登陆页。
<activity android:name="ParaSetActivity"></activity> 这句是设置界面的activity
<activity android:name="MainActivity"></activity> 这句是主界面的activity