<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E6E6E6" android:orientation="vertical" android:padding="10dp"> <ImageView android:layout_width="70dp" android:layout_height="70dp" android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:src="@drawable/qq" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:id="@+id/tv_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="账号:" android:textColor="#000" android:textSize="20sp" /> <EditText android:id="@+id/et_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:padding="10dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="密码:" android:textColor="#000" android:textSize="20sp" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:inputType="textPassword" android:padding="10dp" /> </LinearLayout> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:background="#3C8DC4" android:text="登录" android:textColor="@android:color/white" android:textSize="20sp" /> </LinearLayout>
package com.example.homework9;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private EditText et_account;
private EditText et_password;
private Button btn;
private SharedPreferences sp;
private FileInputStream fi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_account=(EditText) findViewById(R.id.et_account);
et_password=(EditText) findViewById(R.id.et_password);
btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String s1=et_account.getText().toString();
String s2=et_password.getText().toString();
if (TextUtils.isEmpty(s2+s1)) {
Toast.makeText(this, "请输入QQ账号", 0).show();
}else {
sp=getSharedPreferences("data.txt", MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("s1", null);
editor.putString(s2, null);
editor.commit();
Toast.makeText(this, "登陆成功", 0).show();
String s3=sp.getString("s1", "");
String s4=sp.getString("s2", "");
Toast.makeText(this, "保存成功"+s1, 0).show();
}
}
}