选项​​菜单上的切换无线状态 [英] Toggling state of Wifi on Options menu

查看:224
本文介绍了选项​​菜单上的切换无线状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想切换在Android的选项菜单中的WiFi状态。我想通过显示两个图标一个是开启,另一个为关闭,以扩展其功能。它应以这样的方式工作,当用户presses菜​​单键:


  1. 他应该看到开或关
  2. 无线现状
  3. 的图标必须根据其开状态显示或关

  4. 当用户点击了WIFI选项应该切换无线的状态,也相应更改图标

这是可能的选项菜单办?

任何想法,如何根据其状态更改图标?

到目前为止,我能够通过采取用户到不同的活动来切换无线状态。但是,如果我可以在选项菜单中实现它。它将使应用程序更易于使用。

感谢您的输入和您的时间。


解决方案

这里是code支持WiFi开/关:

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.menu,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
    案例R.id.wifistate:
        最后WifiManager无线=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        如果(wifi.isWifiEnabled()){
            wifi.setWifiEnabled(假);
            可绘制可绘制= getResources()getDrawable(R.drawable.off)。
            item.setIcon(绘制);
        }其他{
            可绘制可绘制= getResources()getDrawable(R.drawable.on)。
            item.setIcon(绘制);
            wifi.setWifiEnabled(真);
        }
        打破;    }
    返回true;

}

的manifest.xml(添加权限)

 <使用许可权
    机器人:名字=android.permission.ACCESS_WIFI_STATE/>

menu.xml文件文件:

<?pre> &LT; XML版本=1.0编码=UTF-8&GT;&LT;菜单的xmlns:机器人=htt​​p://schemas.android.com/apk / RES / Android的&GT;&LT;项目机器人:ID =@ + ID / wifistate机器人:标题=关机器人:图标=@绘制/关/&GT;&LT; /菜单&gt;

I would like to toggle the state of Wifi in options menu in Android. I would like to extend its functionality by showing two icons one for "switched on" and the other for "switched off". It should work in such a way that when a user presses the menu key:

  1. He should see current status of Wifi "on" or "off"
  2. The icons must be shown according to its state "on" or "off"
  3. When the user clicks the Wifi option it should toggle the state of Wifi and also change the icon accordingly

Is this possible to do in the options menu ?

Any ideas as to how to change the icons according to its state ?

So far I am able to toggle the Wifi state by taking the user to a different activity. But if I can achieve it in the options menu. It would make the app more easy to use.

Thank you for your input and for your time.

解决方案

here is the code for wifi on/off :

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.wifistate:
        final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if(wifi.isWifiEnabled()){
            wifi.setWifiEnabled(false);
            Drawable drawable = getResources().getDrawable(R.drawable.off);
            item.setIcon(drawable);
        }else{
            Drawable drawable = getResources().getDrawable(R.drawable.on);
            item.setIcon(drawable);
            wifi.setWifiEnabled(true);
        }
        break;

    }
    return true;

}

manifest.xml (add permission)

<uses-permission
    android:name="android.permission.ACCESS_WIFI_STATE" />

menu.xml file :

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/wifistate"  android:title="Off" android:icon="@drawable/off"/></menu>

这篇关于选项​​菜单上的切换无线状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆