کلیدستان

نسخه‌ی کامل: ساخت بخش تنظیمات (در برنامه نویسی اندروید)
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
سلام دوستان

چطوری میشه بخش تنظیمات را به همه اکتیوتی های برنامه بدون استفاده از کد اضافی در اکتیوتی ها ، متصل کرد که بلافاصله بعد از تغییرات صفحات تازه سازی بشند بخصوص لیست ویو  و تولبار؟

با تشکر
(۱۳۹۴/۰۶/۲۰, ۰۱:۳۱ ب.ظ)'osam20' نوشته: [ -> ]سلام دوستان

چطوری میشه بخش تنظیمات را به همه اکتیوتی های برنامه بدون استفاده از کد اضافی در اکتیوتی ها ، متصل کرد که بلافاصله بعد از تغییرات صفحات تازه سازی بشند بخصوص لیست ویو  و تولبار؟

با تشکر
 

سلام 
تا اونجا که من اطلاع دارم باید تمامی موارد در فایل جاوای مربوط به هر اکتیویتی معرفی بشند . که فکر نکنم کار سختی باشه.
 جواب شما در لینک زیر هستش

http://openandroid.ir/2015/08/%d8%b3%d9%...%a7%d8%aa/
اگر پروژه شما محنوا محور از روش زیر استفاده کن

یک کلاس برای سفارشی کردن متن ایجاد کن

کد پی‌اچ‌پی:
public class CustomTextView extends TextView {
    private static final 
String FONT "font";
    private static final 
String SIZE "size";
    
SharedPreferences sp;
    
    private 
void inti(Context context) {
        
sp=PreferenceManager.getDefaultSharedPreferences(context);
        
Typeface face=Typeface.createFromAsset(context.getAssets(), sp.getString(FONT"BZar.ttf")); 
        
this.setTypeface(face); 
        
this.setTextSize(sp.getInt(SIZE22));
    }
   public 
CustomTextView(Context context) {
     
super(context);
     
inti(context);
        }

   

    public 
CustomTextView(Context contextAttributeSet attrs) {
       
super(contextattrs);
       
inti(context);
   }

   public 
CustomTextView(Context contextAttributeSet attrsint defStyle) {
       
super(contextattrsdefStyle);
       
inti(context);
   }

   protected 
void onDraw (Canvas canvas) {
       
super.onDraw(canvas);
       
inti(getContext());

      
   }




لایوت هایی که می خواهیم TextView قرار دهید به صورت زیر عمل کنید

کد پی‌اچ‌پی:
<com.example.android.CustomTextView
       
       android
:id="@+id/txt"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text=".کلیدستان"
       
android:textColor="#000000" /> 

یک لایوت بسازید

کد پی‌اچ‌پی:
- <LinearLayout 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" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SettingActivity">
  <
com.example.sa.CustomTextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="سلام ایرانی" android:textColor="#000000" /> 
  <
SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" /> 
- <
RadioGroup android:id="@+id/radioGroup1" android:layout_width="match_parent" android:layout_height="wrap_content">
  <
RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Bzar" android:onClick="onFontClick" /> 
  <
RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BFANTEZY" android:onClick="onFontClick" /> 
  </
RadioGroup>
  </
LinearLayout

یک کلاس برای بخش تنظیمات ایجاد کنید

کد پی‌اچ‌پی:
SettingActivity extends Activity {

    private static final 
String SIZE "size";
   private static final 
String FONT "font";
   private 
SeekBar                       seekbar;
   private 
SharedPreferences sp;
   private 
TextView txt_sample;
   @
Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_setting);
       
sp=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        
seekbar = (SeekBarfindViewById(R.id.seekBar1);
           
seekbar.setMax(50);
           
seekbar.setProgress(sp.getInt(SIZE22));
           
txt_sample = (TextViewfindViewById(R.id.txt);
           
txt_sample.setTextSize(sp.getInt(SIZE22));
           
           
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

               @
Override
               
public void onStopTrackingTouch(SeekBar seekBar) {
                   
//add here your implementation 
               
}



               @
Override
               
public void onStartTrackingTouch(SeekBar seekBar) {
                   
//add here your implementation
               
}



               @
Override
               
public void onProgressChanged(SeekBar seekBarint progress,
                                             
boolean fromUser) {
                 
//  txt_sample.setTextSize(progress);
                   
sp.edit().putInt(SIZEprogress).commit();
               }
           });
   }



   public 
void onFontClick(View v) {

      
RadioGroup rg = (RadioGroupv.getParent();
      
String selectItem ="BZar.ttf";

      switch (
rg.getCheckedRadioButtonId()) {
          case 
R.id.radio0:
              
selectItem "BZar.ttf";
              break;
          case 
R.id.radio1:
              
selectItem ="BFANTEZY.TTF";
              break;
      }
      
sp.edit().putString(FONTselectItem).commit();
  }


با این روش دیگر نیاز به نوشتن کد های اضافی نیست و
 تغییرات با تعریف کلاس CustomTextView اعمال می شود