使用开关按钮在另一个活动中设置文本可见性



是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);
        });
}

在共享首选项中保存设置

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);
                                                }
                                            }
        }

如果您正在寻找如何将额外信息传递给QuizActivity此处如何使用额外的信息:

Intent intent = new Intent();
intent.putExtra("stateOfTheSwitch","clicked");
startActivity(intent);

和在测验中您可以检索到这样的额外:

if(getIntent().getExtras.getString("stateOfTheSwitch").equals("clicked")
//do something
else
//do something else

最新更新