作业9
package com.example.fg; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et1; private EditText et2; private CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et1=(EditText) findViewById(R.id.et1); et2=(EditText)findViewById(R.id.et2); cb=(CheckBox)findViewById(R.id.cb); SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE); et1.setText(sp.getString("name", "")); et2.setText(sp.getString("password", "")); } public void CCC(View view) { if ("gsf".equals(et1.getText().toString()) && "666".equals(et2.getText().toString())) { if (cb.isChecked()) { SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("name", et1.getText().toString()); editor.putString("password", et2.getText().toString()); editor.commit(); Toast.makeText(this, "登陆成功,已保存", Toast.LENGTH_SHORT).show(); } else { SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("name", ""); editor.putString("password", ""); editor.commit(); Toast.makeText(this, "登陆成功,未保存", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "密码错误,请重试", Toast.LENGTH_SHORT).show(); et1.setText(""); et2.setText(""); } } }
<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="com.example.fg.MainActivity" > <ImageView android:id="@+id/iv_head" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:src="@drawable/dddss" /> <TextView android:id="@+id/tv1" android:layout_width="60dp" android:layout_height="20dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="104dp" android:text="账号" /> <TextView android:id="@+id/tv2" android:layout_width="60dp" android:layout_height="20dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="154dp" android:text="密码" /> <EditText android:id="@+id/et1" android:layout_width="90dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="104dp" android:layout_marginLeft="95dp" android:ems="10" /> <EditText android:id="@+id/et2" android:layout_width="90dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="95dp" android:layout_marginTop="154dp" android:inputType="textPassword" /> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="204dp" android:text="记住密码" android:textSize="20sp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="284dp" android:onClick="CCC" android:text="登录" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="284dp" android:layout_marginLeft="95dp" android:text="取消" /> </RelativeLayout>