50,530
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="设置">
<PreferenceCategory android:title="软件相关">
<CheckBoxPreference android:title="开启密码" android:key="password_set" android:summary="使用密码保护日记本不被偷窥" android:defaultValue="false" />
<EditTextPreference
android:defaultValue="0000"//默认密码
android:password="true"
android:key="password"
android:dependency="password_set"//表示该项取决于上面的开启密码,唯有上面的开启密码选项被打开了,修改密码才能使用
android:summary="修改密码"
android:title="**密码" />
</PreferenceCategory>
</PreferenceScreen>
import android.content.*;
import android.os.*;
import android.preference.*;
import android.widget.*;
import com.wu.mynotes.*;
public class ways extends PreferenceActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
//判断是否开启密码保护是不需要进入任何布局界面的
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);//这一项是获取是否打开密码保护的boolean
Boolean password_set = settings.getBoolean("password_set", true);
if(password_set){//若打开则进入密码输入界面
Intent intent = new Intent(ways.this,locked.class);
startActivity(intent);
this.finish();
}else{//若没有开启密码保护则直接进入程序真正的主界面
Intent intent = new Intent(ways.this, MainActivity.class);
startActivity(intent);
this.finish();
}
}
/*
密码方法:
进入设置界面,
获取是否开启密码,
是则进入密码输入界面,
否则直接进入主界面。
*/
}
<?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:orientation="vertical"
android:gravity="center"
android:id="@+id/locked"
android:background="@drawable/bg1">
<TextView
android:layout_height="wrap_content"
android:text="密码"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"/>
<EditText
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="@+id/lockedpassword"
android:ems="10"
android:layout_width="fill_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:text="确认"
android:layout_width="wrap_content"
android:onClick="passwordcenter"/>
<Button
android:layout_height="wrap_content"
android:text="清除"
android:layout_width="wrap_content"
android:layout_marginLeft="160dp"
android:onClick="passwordclean"/>
</LinearLayout>
</LinearLayout>
import android.app.*;
import android.os.*;
import android.preference.*;
import android.content.*;
import android.view.*;
import android.widget.*;
public class locked extends Activity
{
ways ways;
private EditText password;
private long oldTime = 0;
private static final long EXIT_TIME = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locked);
password =(EditText)findViewById(R.id.lockedpassword);
};
public void passwordcenter(View view){
// return;
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);//获取设置了的密码
String pw = password.getText().toString();//获取用户输入的密码
String passwords = settings.getString("password", "0000");//这里的0000可以随意改为其它值,没有任何作用的
//对俩者进行判断
if (pw != null && pw.equals(passwords)) {
Toast.makeText(locked.this, "密码正确!", Toast.LENGTH_SHORT).show();
//密码输入正确则进入程序的主要界面了
Intent intent = new Intent(locked.this,MainActivity.class);
startActivity(intent);
locked.this.finish();
} else {//密码错误则清空后重新输入,直至正确了为止
Toast.makeText(locked.this, "密码错误!", Toast.LENGTH_SHORT).show();
password.setText("");
} };
public void passwordclean(View view){
password.setText("");
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
long currentTime=System.currentTimeMillis();
if(currentTime - oldTime <= EXIT_TIME){
this.finish();
}else{
Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
oldTime = currentTime;
}
}
return true;
}
}
addPreferencesFromResource(R.xml.preference);