public class MainActivity extends AppCompatActivity { EditText ueditText, peditText; CheckBox checkBox; Button button; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ueditText = (EditText) findViewById(R.id.ed_user); peditText = (EditText) findViewById(R.id.ed_password); checkBox= (CheckBox) findViewById(R.id.cb_remember); button= (Button) findViewById(R.id.button); //使用SharePreferences取出保存的数据,并把数据显示在手机屏幕上 //初始化数据 SharedPreferences sharedPreferences=getSharedPreferences("config",0); //取出数据,如果取出的数据时空时,只需把getString("","")第二个参数设置成空字符串就行了,不用在判断 String name=sharedPreferences.getString("name",""); String password=sharedPreferences.getString("password",""); //获取勾选的状态 boolean checkbox=sharedPreferences.getBoolean("checkbox",false); ueditText.setText(name); peditText.setText(password); checkBox.setChecked(checkbox); } //使用Sharepreferences进行保存数据 public void login(View view){ //获取密码和用户名 String username=ueditText.getText().toString(); String passwowrd=peditText.getText().toString(); //文本判断是否为空,新的API:TextUtils.isEmty() if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(passwowrd)){ Toast.makeText(MainActivity.this,"用户名和密码不能为空",Toast.LENGTH_LONG).show(); }else{ System.out.println("以后补上"); if (checkBox.isChecked()){ //把密码和用户名存起来 //getSharedPreferences(name,model);,name 会生成一个xml文件,model :模式,可读可写等模式 SharedPreferences sp=getSharedPreferences("config",0); SharedPreferences.Editor editor=sp.edit(); //把数据进行保存 editor.putString("name",username); editor.putString("password",passwowrd); //记住勾选的状态 editor.putBoolean("checkbox",checkBox.isChecked()); //提交数据 editor.commit(); }else{ Toast.makeText(MainActivity.this,"未勾选",Toast.LENGTH_LONG).show(); } } } /** * Sharepreference使用的步骤 * 1.获取sp的实例 * Sharepreference sp=getSharepreference(name,model); * 2.获取编辑器 * Editor editor=sp.edit(); * 3.存数据 * editor.putString(name,值) * 4.提交 * editor.commit(); */}xml文件
<LinearLayout 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:orientation="vertical" tools:context="com.hx.myapplication.MainActivity"> <EditText android:id="@+id/ed_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/husername"/> <EditText android:id="@+id/ed_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" android:hint="@string/hpsword"/> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:layout_marginTop="20dp" android:id="@+id/cb_remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/remember"/> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/cb_remember" android:text="@string/login" android:onClick="login"/> </RelativeLayout></LinearLayout>
时间: 2024-08-27 15:51:44