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

时间:2017-12-26 07:45:13

标签: java android onactivityresult

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

        });

}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

在共享首选项中保存设置

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

在您的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);

                                                }

                                            }
        }
相关问题