lundi 2 mai 2016

Saving checkbox state when i open my app again

How can i save my checkbox state. When i open again my app, how can i see my previous checked checkbox? I also want to set a button to open directly a layout via MenuActivity, not an activity which will open the layout, is this posible?

MenuActivity.java

package com.helloworld.practice.practice4;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;public class MenuActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_menu); // Αναζητούμε τα στοιχεία του κουμπιού
    Button button1 = (Button) findViewById(R.id.button1);// Πατώντας το button1 ανοίγει μία νέα δραστηριότητα
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);

    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), Activity1.class);
            startActivity(intent);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), Activity2.class);
            startActivity(intent);
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            {finish();}
        }
    });}}

content_Menu.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0099CC">

<Button
    android:id="@+id/button1"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="@string/button1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/checkBox1"
    android:layout_toStartOf="@+id/checkBox1" />
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox1"
    android:checked="false"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button2"
    android:layout_below="@+id/button1"
    android:layout_toLeftOf="@+id/checkBox2"
    android:layout_toStartOf="@+id/checkBox2"/>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox2"
    android:checked="false"
    android:layout_above="@+id/button3"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button android:id="@+id/button3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_end"
    android:layout_below="@+id/button2"
    android:layout_centerHorizontal="true" /></RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire