🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Android / AYARLAR KISMI CHECKBOX,RADİOBUTTON VB (VE OLAYLARI BUNA BAQ SES TİTREŞİM VB..)

 

1-) Android - AYARLAR KISMI CHECKBOX,RADİOBUTTON VB (VE OLAYLARI BUNA BAQ SES TİTREŞİM VB..)

1-)res klasorune sağ tık-->new resource direktr(üstten 2.)-->raw seç-->müzik seslerini içine at

 

2-)AndroidManifest.xml içi

 

   <?xml version="1.0" encoding="utf-8"?>

 

   <manifest xmlns:android="http://schemas.android.com/apk/res/android"

 

    package="com.example.ramazan.myapplication" >

 

   <uses-permission android:name="android.permission.VIBRATE"/>

 

    <application

 

        android:allowBackup="true"

 

        android:icon="@mipmap/ic_launcher"

 

        android:label="@string/app_name"

 

        android:theme="@style/AppTheme" >

 

        <activity

 

            android:name=".MainActivity"

 

            android:label="@string/app_name" >

 

            <intent-filter>

 

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

 

            </intent-filter>

 

        </activity>

 

        <activity android:name=".Ayarlar"/>

 

    </application></manifest>

 

3-)activity.xml içi

 

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

 

    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=".MainActivity"

 

    android:id="@+id/rl"    >

 

    <TextView

 

        android:layout_width="wrap_content"

 

        android:layout_height="wrap_content"

 

        android:text="@string/hello_world"

 

        android:id="@+id/textView" />

 

    <Button

 

        android:layout_width="wrap_content"

 

        android:layout_height="wrap_content"

 

        android:id="@+id/gonder"

 

        android:layout_below="@+id/textView"

 

        android:layout_centerHorizontal="true"

 

        android:layout_marginTop="138dp"

 

        android:text="0"

 

        android:textSize="30dp"

 

        android:onClick="butondokun" />

 

    </RelativeLayout>

 

4-)strings.xml içi

 

   <resources>

 

    <string name="app_name">My Application</string>

 

 

 

    <string name="hello_world">Hello world!</string>

 

    <string name="action_settings">Settings</string>

 

    <string-array name="renkler">

 

        <item>kırmızı</item>

 

        <item>sarı</item>

 

        <item>yeşil</item>

 

        <item>siyah</item>

 

        <item>beyeaz</item>

 

        <item>turuncu</item>

 

    </string-array>

 

    <string-array name="renk_pozisyon">

 

        <item>0</item>

 

        <item>1</item>

 

        <item>2</item>

 

        <item>3</item>

 

        <item>4</item>

 

        <item>5</item>

 

    </string-array>

 

  </resources>

 

5-)sol taraftaki res yazan klasore sağ tık-->new-->Android resource directy(üstten 2 inci)-->type=xml,name=xml

 

5-)oluşan xml klasorune-->sağtık-->new en üstteki-->ayarlar.xml oluştur içi

 

  <?xml version="1.0" encoding="utf-8"?>

 

  <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

 

    <CheckBoxPreference

 

        android:key="titresim"

 

        android:summaryOff="Titreşim kapalı"

 

        android:summaryOn="Titreşim Açık"

 

        android:title="Tirreşim" />

 

    <CheckBoxPreference

 

        android:key="ses"

 

        android:summaryOff="ses kapalı"

 

        android:summaryOn="ses Açık"

 

        android:title="ses" />

 

    <ListPreference

 

        android:entries="@array/renkler"

 

        android:entryValues="@array/renk_pozisyon"

 

        android:key="arka_plan"

 

        android:summary="Arka plan rengini seçiniz"

 

        android:title="Arka Plan" />

 

  </PreferenceScreen>

 

6-)MainActivity.java yani OnCreatın bulundugu kısım

 

   public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener{

 

    public Button btn;

 

    public  int count;

 

    Vibrator titresim;

 

    RelativeLayout arka_plan;

 

    SharedPreferences preferences,ayarlarr;

 

   Boolean ses_durumu,titresim_durumu;

 

    @Override

 

    protected void onPause() {

 

        SharedPreferences.Editor editor = preferences.edit();

 

        editor.putInt("anahtar_sayac", count);

 

        editor.commit();

 

        super.onPause();    }

 

    MediaPlayer ses;

 

    @Override

 

    protected void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

 

        setContentView(R.layout.activity_main);

 

        arka_plan= (RelativeLayout) findViewById(R.id.rl);

 

        btn=(Button) findViewById(R.id.gonder);

 

        preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

 

    ses=MediaPlayer.create(getApplicationContext(), R.raw.button3);

 

  titresim= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

 

        ayarlarr = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

 

        ayarlari_yukle();

 

        count=preferences.getInt("anahtar_sayac", count);

 

        btn.setText(String.valueOf(count));    }

 

    private void ayarlari_yukle() {

 

        String pos=ayarlarr.getString("arka_plan","3");//ayarlar.xml deki "arka_plan" ve strings.xml deki 3 sayısı

 

        switch (Integer.valueOf(pos)){

 

            case  0:

 

                arka_plan.setBackgroundColor(Color.BLUE);

 

                break;

 

            case  1:

 

                arka_plan.setBackgroundColor(Color.WHITE);

 

                break;

 

            case  2:

 

                arka_plan.setBackgroundColor(Color.RED);

 

                break;

 

            case  3:

 

                arka_plan.setBackgroundColor(Color.GREEN);

 

                break;

 

            case  4:

 

                arka_plan.setBackgroundColor(Color.CYAN);

 

                break;

 

            case  5:

 

                arka_plan.setBackgroundColor(Color.BLACK);

 

                break;        }

 

        ses_durumu=ayarlarr.getBoolean("ses",false);//ayarlar.xml deki key="ses"

 

        titresim_durumu=ayarlarr.getBoolean("titresim",false);//ayarlar.xml deki key="titresim"

 

  ayarlarr.registerOnSharedPreferenceChangeListener(MainActivity.this);    }

 

    public void butondokun(View v) {

 

  if(ses_durumu==true){

 

    ses.start();}

 

        if(titresim_durumu==true){

 

            titresim.vibrate(250);        }

 

        btn.setText(String.valueOf(++count));    }

 

    @Override

 

    public boolean onCreateOptionsMenu(Menu menu) {

 

        // Inflate the menu; this adds items to the action bar if it is present.

 

        getMenuInflater().inflate(R.menu.menu_main, menu);

 

        return true;    }

 

    @Override

 

    public boolean onOptionsItemSelected(MenuItem item) {

 

        // Handle action bar item clicks here. The action bar will

 

        // automatically handle clicks on the Home/Up button, so long

 

        // as you specify a parent activity in AndroidManifest.xml.

 

        int id = item.getItemId();

 

 

 

        //noinspection SimplifiableIfStatement

 

        if (id == R.id.action_settings) {

 

            Intent ıntent=new Intent(getApplicationContext(),Ayarlar.class);

 

            startActivity(ıntent);

 

            return true;        }

 

        return super.onOptionsItemSelected(item);    }

 

    @Override

 

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

 

        ayarlari_yukle();    }}

 2021 Ocak 18 Pazartesi
 350