切换按钮可在webview中启用/禁用图像

时间:2014-08-08 02:25:31

标签: android button toggle

如何使用切换按钮启用或禁用在webview中加载图像?我已经使用了这段代码,但图像永远都是启用的。

   toggle=(ToggleButton)findViewById(R.id.tglbtn1);
        toggle.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
               // TODO Auto-generated method stub
              if(toggle.isChecked())
              {
               Toast.makeText(getApplicationContext(), "The state is changed to on", Toast.LENGTH_LONG).show();
               webView.getSettings().setLoadsImagesAutomatically(true);// Enable Image
                // Loading


              }
                else
                {
                  Toast.makeText(getApplicationContext(), "The state is changed to off", Toast.LENGTH_LONG).show();
                  webView.getSettings().setLoadsImagesAutomatically(false);// Enable Image
                    // Loading

                }
                 }
                 });

1 个答案:

答案 0 :(得分:0)

在android中实现切换按钮的更好方法,

布局xml,

<ToggleButton
                android:id="@+id/leave_toggle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/custom_toggle"
                android:minHeight="1dp"
                android:minWidth="1dp"
                android:textOff=""
                android:textOn="" />

Drawable xml, @绘制/ custom_toggle

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/notification_on" android:state_checked="true"/>
    <item android:drawable="@drawable/notification_off" android:state_checked="false"/>

</selector>

notification_on和notification_off是图像

内部活动,

private ToggleButton getToggleButton(int id) {
        return (ToggleButton) findViewById(id);
    }

将oncheckedchagelistener设置为切换按钮,当用户启用或禁用带缺陷的按钮时,它会通知

 getToggleButton(R.id.leave_toggle).setOnCheckedChangeListener(
                    new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton toggleButton,
                                boolean isChecked) {
                            if(isChecked)
                           {
                            // enabled
                             }else
                                  {
                           //disabled
                                  }
                        }
                    });