解析Android自带的SettingActivity——>Proference

1、在res/xml中 的代码解析

① Preference是采用SharedPreference保存数据的,这里的属性key表示默认存储的键 ,defaultValue表示默认存储的值
②如果是使用preference的话,样式的编写时存放在res/xml中进行编写
 <EditTextPreference
            app:key="signature"
            app:title="@string/signature_title"
            app:useSimpleSummaryProvider="true" />
EditTextPreference: 与EditText的作用一样,存在弹窗,确定则显示输入的值,取消则显示的值不变。
呈现效果如下所示:

在这里插入图片描述

其中title定义的时Your signature , 而useSimpleSummaryProvider属性控制的输入的值是否显示即Not set的显示。

如下所示:

在这里插入图片描述

对于输入的值已经封装,如果想要获取具体的值可以通过方法provideSummary(editTextPreference)进行拿取。

所有res/xml中的代码

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory app:title="@string/messages_header">

        <EditTextPreference
            app:key="signature"
            app:title="@string/signature_title"
            app:useSimpleSummaryProvider="true" />

        <ListPreference
            app:defaultValue="reply"
            app:entries="@array/reply_entries"
            app:entryValues="@array/reply_values"
            app:key="reply"
            app:title="@string/reply_title"
            app:useSimpleSummaryProvider="true" />

    </PreferenceCategory>

    <PreferenceCategory app:title="@string/sync_header">

        <SwitchPreferenceCompat
            app:key="sync"
            app:title="@string/sync_title" />

        <SwitchPreferenceCompat
            app:dependency="sync"
            app:key="attachment"
            app:summaryOff="@string/attachment_summary_off"
            app:summaryOn="@string/attachment_summary_on"
            app:title="@string/attachment_title" />

    </PreferenceCategory>


</PreferenceScreen>

效果为:

在这里插入图片描述

ListPreference的使用

  <ListPreference
            app:defaultValue="reply"
            app:entries="@array/reply_entries"
            app:entryValues="@array/reply_values"
            app:key="reply"
            app:title="@string/reply_title"
            app:useSimpleSummaryProvider="true" />
key属性表示展示的具体值,传入sharedepreference的值,defaultValue属性传入sharedepreference的键,sharedepreference是以键值对的形式存在。entries表示显示的列表,entryValues表示保存显示列表entries的值。

在value/string中存储的数据

<resources>
    <!-- Reply Preference -->
    <string-array name="reply_entries">
        <item>Reply</item>
        <item>Reply to all</item>
    </string-array>

    <string-array name="reply_values">
        <item>reply</item>
        <item>reply_all</item>
    </string-array>
</resources>

效果展示

在这里插入图片描述

SwitchPreference的使用

 <PreferenceCategory app:title="@string/sync_header">

        <SwitchPreferenceCompat
            app:key="sync"
            app:title="@string/sync_title" />

        <SwitchPreferenceCompat
            app:dependency="sync"
            app:key="attachment"
            app:summaryOff="@string/attachment_summary_off"
            app:summaryOn="@string/attachment_summary_on"
            app:title="@string/attachment_title" />

    </PreferenceCategory>
根据switch的状态进行修改summaryOff、summaryOn,表示的是下方的小字
效果展示:直接在sharedpreference中已经保存了switch的状态。

在这里插入图片描述

2、activity.xml

代码展示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
保存fragment的数据

3、activity.java代码数据

public class SettingsActivity extends AppCompatActivity {
	private final static String TAG = "SettingsActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings_activity);
		//fragment的管理
		if (savedInstanceState == null) {
			getSupportFragmentManager()
					.beginTransaction()
					.replace(R.id.settings, new SettingsFragment())
					.commit();
		}
		//返回键的作用
		ActionBar actionBar = getSupportActionBar();
		if (actionBar != null) {
			//给左上角图标的左边加上一个返回的图标 。对应ActionBar.DISPLAY_HOME_AS_UP
			actionBar.setDisplayHomeAsUpEnabled(true);
		}

	}
	//对返回键启用一个监听,实现效果
	@Override
	public boolean onOptionsItemSelected(@NonNull MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				break;
		}
		return true;
	}

	public static class SettingsFragment extends PreferenceFragmentCompat {
		@Override
		public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
			setPreferencesFromResource(R.xml.setting_fragment, rootKey);
		}
	}

}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是一个简单的 Android Studio 的打卡软件的示例代码: 首先,创建一个空的 Android Studio 项目。然后在 activity_main.xml 中添加以下代码: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Welcome to Clock In App!" android:textAlignment="center" android:textSize="24sp" /> <Button android:id="@+id/clockInButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clock In" /> <Button android:id="@+id/clockOutButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clock Out" /> <TextView android:id="@+id/clockInTimeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clock In Time: " /> <TextView android:id="@+id/clockOutTimeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clock Out Time: " /> </LinearLayout> ``` 这个布局包括一个欢迎文本、一个打卡按钮、一个下班打卡按钮和两个文本视图用于显示打卡时间。 接下来,在 MainActivity.java 中添加以下代码: ```java public class MainActivity extends AppCompatActivity { private TextView clockInTimeTextView; private TextView clockOutTimeTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button clockInButton = findViewById(R.id.clockInButton); Button clockOutButton = findViewById(R.id.clockOutButton); clockInTimeTextView = findViewById(R.id.clockInTimeTextView); clockOutTimeTextView = findViewById(R.id.clockOutTimeTextView); clockInButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clockIn(); } }); clockOutButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clockOut(); } }); } private void clockIn() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(new Date()); clockInTimeTextView.setText("Clock In Time: " + currentTime); } private void clockOut() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(new Date()); clockOutTimeTextView.setText("Clock Out Time: " + currentTime); } } ``` 这个 MainActivity 类包括一个 clockIn 方法和一个 clockOut 方法,它们分别会在打卡按钮和下班打卡按钮被按下时调用。这些方法会获取当前时间并将其显示在相应的文本视图中。 现在,运行应用程序并测试它。当你点击“打卡”按钮时,应用程序会显示打卡时间。当你点击“下班打卡”按钮时,应用程序会显示下班打卡时间。 这只是一个简单的打卡应用程序示例。你可以根据自己的需求扩展它,例如添加一个数据库来保存打卡记录,或者添加一个定期提醒用户打卡的功能。 ### 回答2: 好的!下面是一个简单的打卡软件的实现思路。 1. 首先,在Android Studio中创建一个新的项目,命名为"打卡软件"。 2. 创建一个欢迎界面(WelcomeActivity),用于展示打卡软件的Logo和简要介绍。 3. 创建一个登录界面(LoginActivity),用于用户登录。可以使用用户名和密码的形式进行登录验证。可以使用数据库(如SQLite)存储用户的登录信息。 4. 创建一个主界面(MainActivity),用于展示用户的打卡记录和提供打卡功能。 - 在主界面中,可以使用一个RecyclerView来展示用户的打卡记录。创建一个打卡记录实体类(CheckIn)和一个自定义的适配器来绑定RecyclerView。 - 使用SharedPreferences来存储用户的打卡记录,每次用户打卡都将记录保存到SharedPreferences中。 - 在主界面中,可以添加一个打卡按钮,点击后获取当前时间,并将该时间添加到打卡记录中。 5. 创建一个设置界面(SettingActivity),用于设置打卡时间。 - 在设置界面中,可以使用一个TimePicker或DatePicker控件来选择打卡时间。 - 可以使用SharedPreferences来存储用户设置的打卡时间。 6. 添加导航栏(BottomNavigationView或DrawerLayout),在不同的界面之间进行切换。 7. 对于特定的功能,可以创建相应的界面和逻辑进行实现,例如修改密码、查看统计报表等。 8. 进行打卡提醒的设置。 - 使用AlarmManager类创建一个闹钟,设定每天指定时间触发提醒。 - 在接收闹钟触发的广播Receiver中,发送一条通知提醒用户进行打卡。 需要注意的是,以上仅为软件的简单实现思路,具体的实现细节还需要根据具体需求进行进一步开发和完善。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值