英文:
How to create Settings for Switching ON and OFF of 'keep_screen_ON'
问题
这是您要求的翻译部分:
我一直在努力创建一个用于FLAG_KEEP_SCREEN_ON的CheckBox Switch,以便切换屏幕显示的开关。我已经成功创建了它,但它只在一个活动中有效。我想将按钮放在MainActivity中,这样用户可以更轻松地决定是保持屏幕开启还是关闭。我希望它成为整个应用程序中唯一的控制开关。
请任何帮助将不胜感激。
这是我所做的,但在所有活动中不起作用。
这是XML部分
<RelativeLayout
android:id="@+id/mainlayout"
android:background="@drawable/layout_bg2"
android:layout_width="match_parent"
android:layout_height="30sp">
<Switch
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:layout_marginLeft="50dp"
android:id="@+id/ScreenController"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="保持屏幕关闭"
android:layout_centerHorizontal="true"
android:background="@drawable/layout_bg2"
android:layout_marginTop="2dp"
android:layout_toRightOf="@id/ScreenController"
android:layout_marginLeft="30dp"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:fontFamily="serif"
android:id="@+id/StatusText"/>
</RelativeLayout>
这是Java代码部分
MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("保持屏幕开启") ? "保持屏幕关闭" : "保持屏幕开启");
MyText.setTextColor(Color.parseColor("#FFEB3B"));
}else{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("保持屏幕关闭") ? "保持屏幕开启" : "保持屏幕关闭");
MyText.setTextColor(Color.parseColor("#000000"));
}
}
});
英文:
I have been struggling to create a CheckBox Switch for FLAG_KEEP_SCREEN_ON that can switch ON/OFF the screen display. I have managed to create it but it only works for one activity. I want to place the button maybe in the MainActivity where it will be easier for a user to quickly decide to either keep the screen ON/OFF. I want it to be one and only control switch for the entire activities in the app.
Please any help will be much appreciated.
This is what i did that doesn't work as a control settings in all activities.
This is the XML
<RelativeLayout
android:id="@+id/mainlayout"
android:background="@drawable/layout_bg2"
android:layout_width="match_parent"
android:layout_height="30sp">
<Switch
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:layout_marginLeft="50dp"
android:id="@+id/ScreenController"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Keeps Screen OFF"
android:layout_centerHorizontal="true"
android:background="@drawable/layout_bg2"
android:layout_marginTop="2dp"
android:layout_toRightOf="@id/ScreenController"
android:layout_marginLeft="30dp"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:fontFamily="serif"
android:id="@+id/StatusText"/>
</RelativeLayout>
This is the Java code
MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays ON") ? "Screen Light Stays OFF" : "Screen Light Stays ON");
MyText.setTextColor(Color.parseColor("#FFEB3B"));
}else{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays OFF") ? "Screen Light Stays ON" : "Screen Light Stays OFF");
MyText.setTextColor(Color.parseColor("#000000"));
}
}
});
答案1
得分: 1
保存您的变量在共享偏好中,然后创建一个检查该变量并设置标志的活动。
然后,您只需在所有的活动中扩展此活动。
public class Main2Activity extends AppCompatActivity {
String shared_prefs_name = "MY_prefs_name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (screen_on()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public void set_screen_on(boolean screen_on) {
SharedPreferences.Editor saver = getSharedPreferences(shared_prefs_name, MODE_PRIVATE).edit();
saver.putBoolean("screen_on", screen_on);
saver.commit();
if (screen_on) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public boolean screen_on() {
SharedPreferences prefs = getSharedPreferences(shared_prefs_name, MODE_PRIVATE);
return prefs.getBoolean("screen_on", false);
}
}
这样,您可以在您的活动中使用它,像这样:
public class Main3Activity extends Main2Activity {
Switch MySwitch;
TextView MyText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MySwitch = (Switch) findViewById(R.id.ScreenController);
MyText = (TextView) findViewById(R.id.StatusText);
MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
set_screen_on(isChecked);
if (isChecked) {
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays ON") ? "Screen Light Stays OFF" : "Screen Light Stays ON");
MyText.setTextColor(Color.parseColor("#FFEB3B"));
} else {
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays OFF") ? "Screen Light Stays ON" : "Screen Light Stays OFF");
MyText.setTextColor(Color.parseColor("#000000"));
}
}
});
if (screen_on()) {
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays ON") ? "Screen Light Stays OFF" : "Screen Light Stays ON");
MyText.setTextColor(Color.parseColor("#FFEB3B"));
} else {
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays OFF") ? "Screen Light Stays ON" : "Screen Light Stays OFF");
MyText.setTextColor(Color.parseColor("#000000"));
}
}
}
主要是扩展Main2Activity
以获得对screen_on()
布尔值的访问,并使用set_screen_on(boolean)
进行设置。
英文:
save your variable in shared preference then have an activity that checks the variable and sets the flag.
you will then just extend this activity in all your activities.
public class Main2Activity extends AppCompatActivity {
String shared_prefs_name="MY_prefs_name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(screen_on()){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public void set_screen_on( boolean screen_on) {
SharedPreferences.Editor saver = getSharedPreferences(shared_prefs_name, MODE_PRIVATE).edit();
saver.putBoolean("screen_on", screen_on);
saver.commit();
if(screen_on)
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public boolean screen_on() {
SharedPreferences prefs = getSharedPreferences(shared_prefs_name, MODE_PRIVATE);
return prefs.getBoolean("screen_on", false);
}
}
So you can use it in you activity like so .
public class Main3Activity extends Main2Activity {
Switch MySwitch;
TextView MyText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MySwitch=(Switch)findViewById(R.id.ScreenController);
MyText=(TextView) findViewById(R.id.StatusText);
MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
set_screen_on(isChecked);
if(isChecked){
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays ON") ? "Screen Light Stays OFF" : "Screen Light Stays ON");
MyText.setTextColor(Color.parseColor("#FFEB3B"));
}else{
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays OFF") ? "Screen Light Stays ON" : "Screen Light Stays OFF");
MyText.setTextColor(Color.parseColor("#000000"));
}
}
});
if(screen_on())
{
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays ON") ? "Screen Light Stays OFF" : "Screen Light Stays ON");
MyText.setTextColor(Color.parseColor("#FFEB3B"));
}else{
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals("Screen Light Stays OFF") ? "Screen Light Stays ON" : "Screen Light Stays OFF");
MyText.setTextColor(Color.parseColor("#000000"));
}
}
}
Main thing is to extend Main2Activity to get accees to the boolean screen_on()
and to set with set_screen_on(boolean);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论