2017-09-29 6 views
1

설정 기본 설정 화면이 있습니다. ListPreference와 CheckBoxPreference가 있습니다. ListPreference 항목을 선택하면 앱의 날짜 형식을 변경하고 싶습니다. 또한 CheckBoxPreference에 의해 상태 표시 줄에 알림을 표시하거나 숨기려고합니다. 누구든지 그 목표를 달성하기 위해해야 ​​할 일을 말할 수 있습니까?android에서 툴바로 설정을 만드는 방법

또한 환경 설정 화면에 툴바를 어떻게 추가 할 수 있습니까? 나는 여기 붙어있다. 도와주세요. 미리 감사드립니다. 여기에 붙어 있습니다. 도와주세요. 미리 감사드립니다.

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에

<?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 설계 지원 라이브러리를 추가

<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> 

다음으로 활동에 설정해야합니다.

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

다음 onCreateOptionsMenu를 사용하여 설정 메뉴를 추가 할 수 있습니다.

+0

답장을 보내 주셔서 감사합니다. @ ysl. MainActivity 및 기타 활동에 이미 툴바가 있습니다. 문제는 옵션 메뉴에서 "설정"을 클릭하여 설정 화면으로 이동했을 때 툴바가 없다는 것입니다. 이제 위에서 언급 한대로 toolbar.xml을 내 activity_main.xml에 포함하면 내 이전 툴바가 앱 이름을 포함하고 설정 도구 모음이 그 위치를 차지합니다 – Cjsparrow

+0

Activity를 사용하여 설정을 표시하고 싶다면 정상적인 동작을 원할 경우 모든 화면에 대한 도구 모음을 표시하고 조각으로 이동 – YoLo

+0

설정을 위해 조각을 사용하고 있습니다. 내 settingsactivity.java 클래스를 게시했습니다. 그것을보고, 내가해야 할 일을 말해주십시오. – Cjsparrow