我是Android應用程序開發的初學者。我只是想從一個Activity獲取Switch值並將其顯示在另一個Activity中。但是我得到'空'作爲輸出。請幫幫我。如何從一個Activity切換值並將其顯示在其他活動中
Question1.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);
}
});
}
}
Question3.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);
}
}
你可以把錯誤日誌? – FarshidABZ
@Mike M.我已經嘗試過使用'intent.getBooleanExtra()'。但它返回的值與我們給出的值作爲第二個參數相同。這是'intent.getBooleanExtra(「Ans1」,false)'。結果是**錯誤** – Sriram