我的这个应用程序有一个简单的复选框,当用户按下它时,它会在 build.prop 中添加一行,这一切正常,但是当我退出应用程序时,复选框被重置,有没有办法通过添加一个来防止这种情况发生您帖子中的示例代码,因为我还是 Android 应用程序的新手。
主 Activity .java
package com.mythi.tests;
import java.io.DataOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkBox1:
if (checked){
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("cp /system/build.prop /system/build.prop.bak\n");
outputStream.writeBytes("echo 'persist.sys.scrollingcache=3' >> /system/build.prop\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
}catch(InterruptedException e){
}
}else{
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("rm -r /system/build.prop\n");
outputStream.writeBytes("mv /system/build.prop.bak /system/build.prop\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
}catch(InterruptedException e){
}
break;
}
}
}
}
activity_main.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="com.mythi.tests.MainActivity" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="169dp"
android:onClick="onCheckboxClicked"
android:text="CheckBox" />
</RelativeLayout>
最佳答案
使用 getRuntime().exec() 的这种极其奇怪的方法是从哪里得到的?这是使用 SharedPreferences 的简单工作代码示例。用这个替换你的整个 switch 语句:
switch(view.getId()) {
case R.id.checkBox1:
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putBoolean("checkBox1", checked).commit();
break;
}
在 onCreate() 中添加:
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
boolean checked = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean("checkBox1", false);
checkBox1.setChecked(checked);
完成。
关于java - 如何在 Android 应用程序中保存复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25261296/