2017-09-29 6 views
1

私は設定プリファレンス画面を持っています。これには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

+0

どのように制御できませんか? –

答えて

0

あなたの活動。プリファレンス・アクティビティーは他のものと同様に単純なレイアウトを持ち、コンテナー内のプリファレンス・フラグメントを膨らませるだけです。

+0

これを行う方法を教えてもらえますか?私はこの分野の初心者です@ @ Umar – Cjsparrow

0

次に、あなたのactivity.xmlに

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

    tools:context=".MainActivity"> 

    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/tool_bar" 
     ></include> 

    <TextView 
     android:layout_below="@+id/tool_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/TextDimTop" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 
をツールバーが含まれます。レイアウトフォルダ

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/ColorPrimary" 
    android:elevation="4dp" 
    > 
</android.support.v7.widget.Toolbar> 

にtoolbar.xmlを追加するには、build.gradleに

compile 'com.android.support:appcompat-v7:21.0.3' 

をappcompact設計支援ライブラリを追加

あなたの活動にそれを設定する必要があります

toolbar = (Toolbar) findViewById(R.id.tool_bar); 
setSupportActionBar(toolbar); 

をクリックし、onCreateOptionsMenuを使用して設定メニューを追加できます。

+0

あなたの返事ありがとう@ ysl。私はすでに私のMainActivityと他の活動のツールバーを持っています。問題は、オプションメニューの[設定]をクリックして設定画面に移動してもツールバーが表示されない場合です。今、あなたが私のactivity_main.xmlにtoolbar.xmlを含めると上記のように、私の前のツールバーはアプリケーション名を含んでいて、設定ツールバーはその位置をとります – Cjsparrow

+0

あなたは、すべての画面のツールバーを表示してからフラグメントを表示する – YoLo

+0

設定用にフラグメントを使用しています。私はsettingsactivity.javaクラスを投稿しました。それを見て、私が何をしなければならないかを教えてください – Cjsparrow

関連する問題