gpt4 book ai didi

java - 如何从一个Activity中获取Switch值并显示在另一个Activity中

转载 作者:行者123 更新时间:2023-11-30 10:31:46 25 4
gpt4 key购买 nike

我是 Android 应用程序开发的初学者。我只是想从一个 Activity 中获取 Switch 值并将其显示在另一个 Activity 中。但是我得到的输出是“null”。请帮助我。

问题1.java

public class Question1Activity extends AppCompatActivity {
Switch swi;
Button back1, next1;
String value = "NO";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
swi = (Switch) findViewById(R.id.Switch_Ques_1);
back1 = (Button) findViewById(R.id.Button_back1);
next1 = (Button) findViewById(R.id.Button_next1);
swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {

if (isChecked) {
swi.setText("YES");
value="YES";
swi.setTextColor(Color.GREEN);
} else {
swi.setText("NO");
value="NO";
swi.setTextColor(Color.RED);
}

}
});
back1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(Question1Activity.this,LoginActivity.class);
startActivity(intent);
}
});
next1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(Question1Activity.this,Question2Activity.class);
intent.putExtra("Ans1",swi.isChecked());
startActivity(intent);
}
});
}
}

问题3.java

public class Question3Activity extends AppCompatActivity {
TextView t1;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question3);
t1 = (TextView) findViewById(R.id.result);
Intent intent = getIntent();
String s1 =(String) intent.getStringExtra("Ans1");
String s2 = intent.getStringExtra("Ans2");
t1.setText(s1+" "+s2);
}
}

最佳答案

写成:

 String r;
if(swi.isChecked())
r = "true";
else
r= "false";
intent.putExtra("Ans1",r);

代替

 intent.putExtra("Ans1",swi.isChecked());

关于java - 如何从一个Activity中获取Switch值并显示在另一个Activity中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43026510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com