【问题标题】:Switch for enable/disable vibration and sound with button使用按钮启用/禁用振动和声音的开关
【发布时间】:2013-11-06 23:02:43
【问题描述】:

使用 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"/>

【问题讨论】:

    标签: android audio vibration


    【解决方案1】:

    如果不告诉我们您遇到了什么错误,就很难说清楚。但有几件事......

    检查您的 xml 文件是否与您设置内容视图 (opzioni.xml) 的名称相同

    setContentView(R.layout.opzioni);
    

    此外,在您的 xml 中,对于您的两个开关,您都设置了对方法的接口调用:

    android:onClick="onSwitchClicked"
    

    它告诉按钮视图对象在被触摸时在您的活动中调用以下方法:

    public void onSwitchClicked(View v)
    

    但是由于你没有定义它,你会得到一个错误。

    因此,要么在您的活动中定义该方法,要么从您的 xml 中删除该行。

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      相关资源
      最近更新 更多