私はint "flag"変数を持っています。これには2つの可能なint値、0と1があります。
ここでのアプリのメイン テーマは次のとおりです。
ユーザーが YES を選択した場合、int=1 を割り当てます。それ以外の場合は、[いいえ] を選択し、int=0 を割り当てます。
私はこれを使用して達成しています:
public static int flag;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.flag0:
flag=0;
System.err.println("Flag : " + flag);
break;
case R.id.flag1:
flag=1;
System.err.println("Flag : " + flag);
break;
default:
break;
}
}
私の問題はここにあります:それは大丈夫です、私のアプリケーションまで設定を保存すると、フォアグラウンドまたはバックグラウンドで実行されます。
一度アプリケーションを閉じたり強制終了したりすると、保存されているすべての設定がクリアされます。つまり、デフォルト値のゼロに設定されます。
STORAGE オプションの作業を開始したばかりなので、SHARED PREFERENCES を実装しようとしましたが、上記の問題はまだあります。共有設定を使用して int 値を保存するために使用した方法は次のとおりです。
編集:以下の回答を試しましたが、無駄でした。ここに投稿しているコードの合計があります。テストするのに役立ちます(多分)
xml ファイル:
<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" >
<TextView
android:id="@+id/text"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button android:id="@+id/flag0"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:text="set to 0"/>
<Button android:id="@+id/flag1"
android:layout_marginTop="10dp"
android:layout_below="@+id/text"
android:layout_toRightOf="@+id/flag0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="set to 1"/>
<Button android:id="@+id/test0"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/flag1"
android:text="Test for 0"/>
主な活動:
public class MainActivity extends Activity implements OnClickListener {
TextView textView;
Button bt1,bt2,testbt1;
public static int flag;
SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text);
bt1 = (Button) findViewById(R.id.flag0);
bt2 = (Button) findViewById(R.id.flag1);
testbt1 = (Button) findViewById(R.id.test0);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
testbt1.setOnClickListener(this);
pref=getSharedPreferences("Sai", MODE_PRIVATE);
pref.getInt("lang_us", 0);
System.err.println("Flag Value in Main method : " + flag);
}
public void saveFlag(){
pref=getSharedPreferences("Sai",MODE_PRIVATE);
Editor editor = pref.edit();
editor.putInt("lang_us", flag);
editor.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.flag0:
flag=0;
saveFlag();
System.err.println("Flag : " + flag);
break;
case R.id.flag1:
flag=1;
saveFlag();
System.err.println("Flag : " + flag);
break;
case R.id.test0:
System.err.println("Flag : " + flag);
if (flag == 0) {
textView.setText("flag is zero");
} else {
textView.setText("flag is not zero");
}
break;
default:
break;
}
}
}
どこが悪いのでしょうか?