Preference框架(登录界面)

这篇博客演示了如何使用Preference框架来实现一个跨应用的登录界面。内容包括以PreferenceScreen为根的XML布局,其中包含两个EditTextPreference用于输入用户名和密码。通过SharedPreference存储用户数据,并通过getPreferences()方法进行检索。注意,此方法适用于API level 11及以上。
摘要由CSDN通过智能技术生成


       设置跨应用程序的Preference登录的demo吧!

        简单介绍:PreferenceScreen是根元素,包含了两个EditTextPreference元素,用于存放用户名和密码,系统生成一个操作preference的UI,用SharedPreference存储,通过调用了getPregerences()检索。

PS:version11以上才可以!

res/xml/Preferences.xml

<span style="font-family:Microsoft YaHei;font-size:18px;"></span><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" >

	<EditTextPreference
	    android:title="用户名:"
	    android:summary="请输入密码:"
	    android:key="username">
	</EditTextPreference>
	<EditTextPreference
	    android:title="密码:"
	    android:summary="请输入密码:"
	    android:password="true"
	    android:key="password">
	</EditTextPreference>
</PreferenceScreen>

 
src/com/superloveboy/MyPreferences.java 

<span style="font-family:Microsoft YaHei;font-size:18px;">package com.superloveboy.datastorage;
import android.os.Bundle;
import android.preference.PreferenceActivity;


public class MyPreferences extends PreferenceActivity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
	}
	
}</span>

src/com/superloveboy/AndroidManifest.xml

<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.superloveboy.preferenceactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".sharedPreferenceDemo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MyPreferences">
        </activity>
    </application>

</manifest></span>

之前嫌麻烦写完不附效果图,发现自己看别人写的demo时,不知道是不是自己想要的效果,就感觉忒坑,试了半天才知道不对!今天附上效果图



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值