[英]Using switch button to set text visibility in another activity
我是android開發的初學者,並且拼命尋找解決我在測驗應用程序中遇到的挑戰的解決方案。 我在SettingsActivity
有一個開關,該開關可在另一個活動中設置可見和不可見的特定文本。 但是我一直在尋找正確的邏輯來從我的SettingsActivity
中引用該文本以切換其可見性時遇到問題。
QuizActivity.java
public class QuizActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mExplanationText = (TextView) findViewById(R.id.txtExplanation); //this is the text with which i want the visibility to be controlled from SettingsActivity
}
}
SettingsActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:paddingTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/explanationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:fontFamily="cursive"
android:layout_marginBottom="25dp"
android:textColor="@color/settings_text"
android:text="@string/explanation"/>
<Switch
android:id="@+id/explanationSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:checked="true"
android:layout_gravity="center"/>
</LinearLayout>
SettingsActivity.java
private Switch mExplanationSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_settings);
mExplanationSwitch = (Switch) findViewById(R.id.explanationSwitch);
mExplanationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// how do i toggle visibility of mExplanation text in my QuizActivity.java from here?
}
});
}
MainActivity.java
//This is where i start Quiz Activity
private Button startQuiz;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startQuiz = (Button) findViewById(R.id.start);
startQuiz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startQuiz = new Intent(this, QuizActivity.class);
startActivity(beginnerIntent);
});
}
如果您正在尋找如何將額外的信息傳遞給QuizActivity,那么在這里如何使用額外的意圖:
Intent intent = new Intent();
intent.putExtra("stateOfTheSwitch","clicked");
startActivity(intent);
在QuizActivity中,您可以像這樣檢索其他內容:
if(getIntent().getExtras.getString("stateOfTheSwitch").equals("clicked")
//do something
else
//do something else
mExplanationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences preferences = getSharedPreferences("SETTINGS", MODE_PRIVATE);
Editor editor = preferences.edit();
if(isChecked){
editor.putBoolean("ntoificatoin",isChecked);
}
else
{
editor.putBoolean("ntoificatoin",isChecked);
}
editor.apply();
// how do i toggle visibility of mExplanation text in my QuizActivity.java from here?
}
});
public class QuizActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mExplanationText = (TextView) findViewById(R.id.txtExplanation); //this is the text with which i want the visibility to be controlled from SettingsActivity
SharedPreferences preferences = getSharedPreferences("SETTINGS", MODE_PRIVATE);
boolean b=preferences.getBoolean("ntoificatoin",false);//it returns stored boolean value else returns false
if(b){
//return true from Settings
//do what you want to
mExplanationText.setText(" "+b);
}
else
{
//return false from Settings
//do what you want to
mExplanationText.setText(" "+b);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.