gpt4 book ai didi

java - 如何在android中使用工具栏创 build 置

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:00 27 4
gpt4 key购买 nike

我有一个“设置首选项”屏幕。它有一个 ListPreference 和一个 CheckBoxPreference。当我选择 ListPreference 的一项时,我想更改应用程序的日期格式。另外,通过 CheckBoxPreference 我想在状态栏上显示/隐藏通知。谁能告诉我我必须做什么才能实现这一目标。

另外,如何将工具栏添加到首选项屏幕?我被困在这里了。请帮忙。提前致谢。我被困在这里了。请帮忙。提前致谢。

MainActivity.java

public void setCurrentDateOnView() {
String dateFormat = "dd - MM - yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US);
tv_Current_Date.setText(simpleDateFormat.format(calendar_now.getTime()));
String short_weekday = new DateFormatSymbols().getShortWeekdays()[day_of_current_week];
tv_Current_weekday.setText(short_weekday);
til_Current_Date.setError(null);
}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case R.id.action_settings:
Intent intent_settings = new Intent(this, SettingsActivity.class);
startActivity(intent_settings);
Toast.makeText(this, "You have clicked on settings action menu.", Toast.LENGTH_SHORT).show();
break;

}
return super.onOptionsItemSelected(item);
}

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {

NotificationManager mNotifyManager;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

boolean notifyEnabled = sharedPreferences.getBoolean("pref_cb_notification", true);
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (notifyEnabled) {
//Show notification
showNotification();
}
else {
//Hide notification
hideNotification();
}
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

boolean isChecked = sharedPreferences.getBoolean("pref_cb_notification", false);
if (isChecked) {
//Show notification
showNotification();
}
else {
//Hide notification
hideNotification();
}

}

public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

}
}

//Method to show notification
public void showNotification() {
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder)
new NotificationCompat.Builder(SettingsActivity.this)
.setSmallIcon(R.drawable.ic_notifications_white_24dp)
.setContentTitle("My Application")
.setSubText("Tap to start");
Intent resultIntent = new Intent(SettingsActivity.this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent
.getActivity(SettingsActivity.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//System.currentTimeMillis();
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
//notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotifyManager.notify(1, notification);
}

//Method to hide notification
public void hideNotification() {
mNotifyManager.cancel(1);
}

}

Settings image

最佳答案

要添加工具栏,您只需要在 Activity 的布局文件中使用协调器布局。首选项 Activity 与其他 Activity 一样具有简单的布局,您只需在容器内膨胀首选项 fragment 即可。

关于java - 如何在android中使用工具栏创 build 置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46481989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com