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

问题描述 投票:0回答:2

我是Android开发的初学者,我正在拼命寻找解决我在测验应用程序上遇到的挑战的解决方案。我在我的SettingsActivity中有一个开关,它可以在另一个可见和不可见的活动中设置特定文本。然而,我一直在找到正确的逻辑从我的SettingsActivity引用其他文本来切换其可见性。

quiz activity.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>

settings activity.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?
            }
        });
}

main activity.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);

        });

}
java android onactivityresult
2个回答
0
投票

Save Setting in Shared Preferences

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

In Your QuizActivity

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

                                                }

                                            }
        }

0
投票

如果您正在寻找如何将额外信息传递给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
© www.soinside.com 2019 - 2024. All rights reserved.