1.知识图谱
例子1:模仿登录的记住密码
XML代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.example.cookie.android0623data.LoginActivity">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/et_login_uname"
- android:hint="请输入用户名"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/et_login_upass"
- android:hint="请输入密码"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="登录"
- android:onClick="login"/>
- </LinearLayout>
Java代码:
- package com.example.cookie.android0623data;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class LoginActivity extends AppCompatActivity {
- private EditText et_login_uname;
- private EditText et_login_upass;
- private SharedPreferences sp;
- private SharedPreferences.Editor editor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- et_login_uname = (EditText) findViewById(R.id.et_login_uname);
- et_login_upass = (EditText) findViewById(R.id.et_login_upass);
- //只能够自己的文件进行读写
- sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
- //想再sp里面写东西
- editor = sp.edit();
- //再次进来,获取数据
- String uname=sp.getString("uname","");
- String upass=sp.getString("upass","");
- et_login_uname.setText(uname);
- et_login_upass.setText(upass);
- }
- public void login(View view){
- //uname=admin upass=123456
- String uname=et_login_uname.getText().toString();
- String upass=et_login_upass.getText().toString();
- Toast.makeText(this, "跳转页面", Toast.LENGTH_SHORT).show();
- editor.putString("uname",uname);
- editor.putString("upass",upass);
- //细节:
- editor.commit();
- }
- }
例二,模仿手机的设置界面中的WLAN的开启和关闭
1.首先,在一个项目的res文件下建一个xml包
2.跳转到project项目下,新建一个setting.xml文件
xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
- <SwitchPreference
- android:title="WLAN"
- android:summaryOn="wifi已开启"
- android:summaryOff="wifi已关闭"
- android:key="@+id/sp_set_wifi"
- ></SwitchPreference>
- </PreferenceScreen>
SettingActivity.java代码如下:
- package com.example.cookie.android0623data;
- import android.os.Bundle;
- import android.os.PersistableBundle;
- import android.preference.PreferenceActivity;
- import android.support.annotation.Nullable;
- /**
- * Created by Administrator on 2017/6/23 0023.
- */
- public class SettinActivity extends PreferenceActivity{
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //指定xml文件
- addPreferencesFromResource(R.xml.setting);
- }
- }