- 论坛徽章:
- 0
|
本帖最后由 rocox 于 2015-05-27 14:28 编辑
关键技术:
用到了本地化控件:SharedPreferences,简单的说就是本地配置。
四大组件:Intent
基本思路请看代码:
Java代码:
- 1 import android.os.Bundle;
- 2 import android.app.Activity;
- 3 import android.content.Context;
- 4 import android.content.Intent;
- 5 import android.content.SharedPreferences;
- 6 import android.view.Menu;
- 7 import android.view.View;
- 8 import android.view.View.OnClickListener;
- 9 import android.widget.Button;
- 10 import android.widget.CheckBox;
- 11 import android.widget.EditText;
- 12 import android.widget.Toast;
- 13
- 14 public class MainActivity extends Activity {
- 15 private Button logIn;
- 16 private CheckBox rember,auto;
- 17 private EditText name,password;
- 18 private SharedPreferences test;
- 19 private SharedPreferences.Editor editor;
- 20 private boolean haved;
- 21 @Override
- 22 protected void onCreate(Bundle savedInstanceState) {
- 23 super.onCreate(savedInstanceState);
- 24 setContentView(R.layout.activity_main);
- 25
- 26 logIn= (Button) findViewById(R.id.button1);
- 27 rember=(CheckBox) findViewById(R.id.remberFlag);
- 28 auto=(CheckBox) findViewById(R.id.autoRunFlag);
- 29 name=(EditText) findViewById(R.id.name);
- 30 password=(EditText) findViewById(R.id.password);
- 31 final Intent intent=new Intent(MainActivity.this,NewMainActivity.class);
- 32 //打开 或 创建一个名为UserData的xml文件
- 33 test=getSharedPreferences("UserData", Context.MODE_PRIVATE);
- 34 editor=test.edit();
- 35
- 36 if(test.getBoolean("auto",false)!=false)
- 37 {
- 38 startActivity(intent);
- 39 }
- 40
- 41 if(test.getBoolean("rember", false)!=false)
- 42 {
- 43 String temp;
- 44 if((temp=test.getString("name", null))!=null)
- 45 name.setText(temp);
- 46 if((temp=test.getString("password",null)) != null)
- 47 password.setText(temp);
- 48 rember.setChecked(true);
- 49 haved=true;
- 50 }
- 51
- 52 logIn.setOnClickListener(new View.OnClickListener() {
- 53 @Override
- 54 public void onClick(View v) {
- 55 if(haved == false )
- 56 {
- 57 if(rember.isChecked())
- 58 {
- 59 editor.putBoolean("rember", true);
- 60 editor.putString("name", name.getText().toString());
- 61 editor.putString("password", password.getText().toString());
- 62 editor.commit();
- 63 }
- 64 if(auto.isChecked()==true)
- 65 editor.putBoolean("auto", true);
- 66 }
- 67 startActivity(intent);
- 68 }
- 69 });
- 70 }
- 71 }
- 复制代码
- activity_main.xml
- 复制代码
- 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 2 xmlns:tools="http://schemas.android.com/tools"
- 3 android:layout_width="match_parent"
- 4 android:layout_height="match_parent"
- 5 android:paddingBottom="@dimen/activity_vertical_margin"
- 6 android:paddingLeft="@dimen/activity_horizontal_margin"
- 7 android:paddingRight="@dimen/activity_horizontal_margin"
- 8 android:paddingTop="@dimen/activity_vertical_margin"
- 9 tools:context=".MainActivity" >
- 10
- 11 <EditText
- 12 android:id="@+id/name"
- 13 android:layout_width="match_parent"
- 14 android:layout_height="wrap_content"
- 15 android:layout_alignLeft="@+id/password"
- 16 android:layout_alignParentTop="true"
- 17 android:layout_marginTop="16dp"
- 18 android:ems="10" />
- 19
- 20 <EditText
- 21 android:id="@+id/password"
- 22 android:layout_width="match_parent"
- 23 android:layout_height="wrap_content"
- 24 android:layout_below="@+id/name"
- 25 android:layout_centerHorizontal="true"
- 26 android:layout_marginTop="18dp"
- 27 android:ems="10" >
- 28
- 29 <requestFocus />
- 30 </EditText>
- 31
- 32 <Button
- 33 android:id="@+id/button1"
- 34 android:layout_width="wrap_content"
- 35 android:layout_height="wrap_content"
- 36 android:layout_alignLeft="@+id/password"
- 37 android:layout_below="@+id/password"
- 38 android:layout_marginLeft="16dp"
- 39 android:text="登陆" />
- 40
- 41 <CheckBox
- 42 android:id="@+id/autoRunFlag"
- 43 android:layout_width="wrap_content"
- 44 android:layout_height="wrap_content"
- 45 android:layout_alignLeft="@+id/password"
- 46 android:layout_below="@+id/button1"
- 47 android:text="自动登陆" />
- 48
- 49 <CheckBox
- 50 android:id="@+id/remberFlag"
- 51 android:layout_width="wrap_content"
- 52 android:layout_height="wrap_content"
- 53 android:layout_alignBaseline="@+id/autoRunFlag"
- 54 android:layout_alignBottom="@+id/autoRunFlag"
- 55 android:layout_alignRight="@+id/password"
- 56 android:text="记住密码" />
- 57
- 58 </RelativeLayout>
- 复制代码
- activity_second.xml:
- 复制代码
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <RatingBar
- android:id="@+id/ratingBar1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="170dp" />
- </RelativeLayout>
复制代码 AndroidMainifest中要注册:- <activity
- android:name="com.example.log_in.NewMainActivity"
- android:label="second_activity" >
- </activity>
复制代码 |
|