본문 바로가기

Android

Preference

Activity_main.xml

 

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

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

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

 

    <EditText

        android:id="@+id/et1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:inputType="text" />

 

    <EditText

        android:id="@+id/et2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:inputType="number" />

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@+id/btn1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="입력" />

 

        <Button

            android:id="@+id/btn2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="출력" />

    </LinearLayout>

 

    <TextView

        android:id="@+id/tv1"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</LinearLayout>

 

 

MainActivity.java

 

package com.example.sharepreferenceex01;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.view.Menu;

import android.view.View;

import android.view.inputmethod.InputMethodManager;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

 

 

public class MainActivity extends Activity {

     InputMethodManager imm;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

 

           imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

 

           findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     EditText et1 = (EditText)findViewById(R.id.et1);

                     EditText et2 = (EditText)findViewById(R.id.et2);

                    

                     imm.hideSoftInputFromWindow(et1.getWindowToken(), 0);

                     imm.hideSoftInputFromWindow(et2.getWindowToken(), 0);

                    

        SharedPreferences sharedPreferences = getSharedPreferences("PrefText", 0);

                     SharedPreferences.Editor editer = sharedPreferences.edit();

                     editer.putString("et1", et1.getText().toString());

                     editer.putString("et2", et2.getText().toString());

                     if(editer.commit()){

Toast.makeText(MainActivity.this, "저장되었습니다.", Toast.LENGTH_SHORT).show();

                     }

                }

           });

 

           findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     TextView tv1 = (TextView)findViewById(R.id.tv1);

                    

       SharedPreferences sharedPreferences = getSharedPreferences("PrefText", 0);

          tv1.append("\n et1 : " + sharedPreferences.getString("et1", "내용없음"));

          tv1.append("\n et2 : " + sharedPreferences.getString("et2", "내용없음"));

                }

           });

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

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

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

           return true;

     }

 

}

 




'Android' 카테고리의 다른 글

PreferenceActivity  (0) 2013.04.04
액티비티(Activity) - 액티비티간의 통신  (0) 2013.04.03
액티비티(activity) -  (0) 2013.04.03
대화상자 - 커스텀대화상자  (0) 2013.04.02
대화상자 - MultiChoiceItems  (0) 2013.04.02