کلیدستان

نسخه‌ی کامل: ساخت تنظیمات
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
با سلام 
میخواستم بپرسم چجوری  صفحه تنظیمات ساخته می شوند 
مهم ترین موضوعی که می خواستم بدونم اینه که چجوری یک دکمه رو غیر فعال می کنیم (با کد نویسی) و از کجا می شه فهمید یه چک باکس در حالت چک هست یا نه و اینکه چجوری میشه یکSwitch رادر نسخه های پایین اندروید هم نمایش داد
با سلام

برای ساخت تنظیمات از SharedPreferences استفاده می شود

اگر یک چک باکس تیک باشد به ما یک true برمی گرداند و اگر نباشد false ما هم ان را با SharedPreferences ذخیره می کنیم

توصیه می کنم درباره ی SharedPreferences تحقیق کنید تا با ان به راحتی بتوانید تنظیمات بسازید
(۱۳۹۵/۰۶/۲۱, ۱۱:۲۴ ق.ظ)علیرضا ارشدی نوشته: [ -> ]با سلام

برای ساخت تنظیمات از SharedPreferences استفاده می شود

اگر یک چک باکس تیک باشد به ما یک true برمی گرداند و اگر نباشد false ما هم ان را با SharedPreferences ذخیره می کنیم

توصیه می کنم درباره ی SharedPreferences تحقیق کنید تا با ان به راحتی بتوانید تنظیمات بسازید
ممنون از کمکتون ولی مقدار true یاfalse را از کجا دریافت کنم
سلام دوست عزیز
من کد تنظیمات که خودم ساختم رو بهتون میدم واس نمونه
فقط من در این تنظیمات  به جای ترو و فلس از اعداد استفاده کردم

یه PreferenceActivity بساز برای ذخیره حالت
کد پی‌اچ‌پی:
public class SaveSetting extends PreferenceActivity {

    
    
    @
Override
    
protected void onCreate(Bundle savedInstanceState) {
        
// TODO Auto-generated method stub
        
super.onCreate(savedInstanceState);
        
        
        
SharedPreferences shere PreferenceManager.getDefaultSharedPreferences(this);
        
        
        
String notifection shere.getString("not""");
        
        
        
        
        
        
        
    }



کد صفحه تنظیمات

کد پی‌اچ‌پی:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
              android:background="@mipmap/back"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToggleButton" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="فعال کردن نوتیفکشن"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ذخیره" />

</LinearLayout> 


کد کلاس صفحه تنظیمات


کد پی‌اچ‌پی:
public class SettingApp extends Activity {

    
SharedPreferences sh;
    
    
String chnum;
    
Button btnsave;
    
ToggleButton ch;
    
Editor ed;
    
Boolean bl;
    private 
String nup;
    @
Override
    
protected void onCreate(Bundle savedInstanceState) {
        
// TODO Auto-generated method stub
        
super.onCreate(savedInstanceState);
        
setContentView(R.layout.setting);

        
SharedPreferences sh2 getSharedPreferences("SaveSetting"MODE_PRIVATE);
        
Editor editorco2 sh2.edit();
        
chnum sh2.getString("not""");
        
         
ch = (ToggleButton)findViewById(R.id.toggleButton1);
            
btnsave = (Button)findViewById(R.id.button1);


if (
chnum.equals("")){

    
chnum "0";
}



        
boolean isChecked false;

        if (
chnum.equals("1")) {
            
ch.setChecked(true);
        }





        
        
        
btnsave.setOnClickListener(new View.OnClickListener() {
            
            @
Override
            
public void onClick(View arg0) {
                
// TODO Auto-generated method stub



                
SharedPreferences sharedata1 getSharedPreferences("SaveSetting"MODE_PRIVATE);
                
Editor ed1 sharedata1.edit();





                if (
ch.isChecked()) {
                    
nup="1";

                } else {
                    
nup="0";
                }


                
ed1.putString("not"nup);

                
Toast.makeText(getApplication(), "دادها ذخیره شد",Toast.LENGTH_LONG).show();




                
ed1.commit();
            }
        });
        
        
        
    }



اجرای کد در اکتیویتی اصلی که چک میکنه اگر عدد ذخیره شده 1 باشه نوتیفکشن فعال میشه اگر 0 باشه غیر فعال میشه

کد پی‌اچ‌پی:
       sh getSharedPreferences("SaveSetting"MODE_PRIVATE);
 
      edsh.edit();
 
    String   tools sh.getString("not""");

if (
tools.equals("1")) {


 
   NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
 
           .setSmallIcon(R.mipmap.ic_launcher// notification icon
 
           .setContentTitle("مطلب تصادفی"// title for notification
 
           .setContentText("برای خواندن این مطلب ضربه بزنید"// message for notification
 
           .setAutoCancel(true); // clear notification after click
 
   Intent browserIntent = new Intent(thisViewnotif.class);

 
   PendingIntent pi PendingIntent.getActivity(this0browserIntentIntent.FLAG_ACTIVITY_NEW_TASK);

 
   mBuilder.setContentIntent(pi);
 
   NotificationManager mNotificationManager =
 
           (NotificationManagergetSystemService(Context.NOTIFICATION_SERVICE);
 
   mNotificationManager.notify(0mBuilder.build());





امیدوارم مشکلتون حل بشه
واقعا ممنون بطور کامل متوجه شدم از از اینکه تجربیاتتون رو در اختیارم گذاشتید سپاسگذارم