我目前在我的一个XML文件中有两个开关。以下是该文件的代码,以防它有用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Countdown Sound Effects"
android:checked="true" />
<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ding Sound Effect when Correct"
android:checked = "true" />
</LinearLayout>
在我的一个课程中,我只想尝试返回每个开关的状态。但是,它始终返回null。这是我的课程代码:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Switch;
public class Settings extends Activity {
private Switch ding;
private Switch countdowneffect;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.optionsactivity);
ding = (Switch) findViewById(R.id.switch2);
countdowneffect = (Switch) findViewById(R.id.switch1);
}
public boolean isDingChecked()
{
return ding.isChecked();
}
public boolean isCountDownChecked()
{
return countdowneffect.isChecked();
}
}
我有什么东西真的很小吗?我似乎无法弄明白。谢谢!
答案 0 :(得分:0)
试用此代码
XML
<RelativeLayout 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"
tools:context=".MainActivity"
android:padding="5dp">
<Switch
android:id="@+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Play with the Switch" />
<TextView
android:id="@+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/mySwitch"
android:layout_marginTop="22dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView switchStatus;
private Switch mySwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchStatus = (TextView) findViewById(R.id.switchStatus);
mySwitch = (Switch) findViewById(R.id.mySwitch);
//set the switch to ON
mySwitch.setChecked(true);
//attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
switchStatus.setText("Switch is currently ON");
}else{
switchStatus.setText("Switch is currently OFF");
}
}
});
//check the current state before we display the screen
if(mySwitch.isChecked()){
switchStatus.setText("Switch is currently ON");
}
else {
switchStatus.setText("Switch is currently OFF");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
<强> optionsactivity.xml 强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>
<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="true"/>
<TextView
android:id="@+id/textSwitch1Value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textSwitch2Value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>
<强> Settings.java 强>
public class Settings extends Activity {
private Switch switch1;
private Switch switch2;
private TextView textSwitch1Value;
private TextView textSwitch2Value;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.optionsactivity);
switch1 = (Switch) findViewById(R.id.switch1);
switch2 = (Switch) findViewById(R.id.switch2);
textSwitch1Value = (TextView) findViewById(R.id.textSwitch1Value);
textSwitch2Value = (TextView) findViewById(R.id.textSwitch2Value);
textSwitch1Value.setText("Switch : "+isSwitch1());
textSwitch2Value.setText("Switch : "+isSwitch2());
}
private boolean isSwitch1(){
return switch1.isChecked();
}
private boolean isSwitch2(){
return switch2.isChecked();
}
}