使用 Android Studio。我有两个按钮,按下时会发出振动和声音。我在布局中放置了两个开关,以启用/禁用两个按钮的振动和声音。我尝试设置代码,但是当我启动应用程序时,它崩溃并出错。有人能帮我吗?
这是代码java:
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.opzioni);
final Switch onOffSwitchvibr = (Switch) findViewById(R.id.switch2);
onOffSwitchvibr.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton View, boolean isChecked) {
if (isChecked) {
onOffSwitchvibr.isEnabled();
// The toggle is enabled
} else {
onOffSwitchvibr.getTextOff();
return;
// The toggle is disabled
}
}
});
}
}
这是代码xml:
<RelativeLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginLeft="40dp"
android:text="SETTINGS"
android:textStyle="bold"
android:textColor="#999999"
android:textSize="15sp"/>
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#B2B2B2"
android:layout_marginTop="65dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"/>
<Switch
android:id="@+id/switch1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/switch1"
android:layout_marginTop="80dp"
android:layout_marginRight="32dp"
android:layout_marginLeft="32dp"
android:onClick="onSwitchClicked"
android:fontFamily="sans-serif"
android:switchTextAppearance="@android:style/TextAppearance"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_marginTop="115dp"
android:background="#999999"/>
<Switch
android:id="@+id/switch2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/switch2"
android:layout_marginTop="130dp"
android:layout_marginRight="32dp"
android:layout_marginLeft="32dp"
android:fontFamily="sans-serif"
android:textOn="ON"
android:textOff="OFF"
android:onClick="onSwitchClicked"
android:switchTextAppearance="@android:style/TextAppearance"/>