[英]Toggle Button Not Working Properly on Bluetooth Adapter
Hi Sir/Ma'am, I'm trying to Enable/Disable Bluetooth on Toggle Button and also there is an additional functionality which ask for password on enable/disable. 尊敬的先生/女士,我正在尝试启用/禁用“切换按钮”上的蓝牙功能,还有一项附加功能要求启用/禁用上输入密码。 Toggle Button is taking default state of Bluetooth at launch(Means if Bluetooth is Enable, then toggle will be set on and if Bluetooth is Disable, toggle will be off). 切换按钮在启动时处于蓝牙的默认状态(意味着如果蓝牙已启用,则切换将设置为打开,如果蓝牙为禁用,则切换将关闭)。
Also, its working fine on when we enter correct password. 另外,当我们输入正确的密码时,它的工作正常。 But, the main problem is that When I enter wrong password, Toggle is not behave as expected. 但是,主要问题是,当我输入错误的密码时,Toggle的行为不符合预期。
What I mean, Suppose Bluetooth is On and I am trying to disable it by my app. 我的意思是,假设蓝牙已打开,而我正在尝试通过我的应用禁用它。 When I click on Toggle, it ask me to enter password. 当我单击切换时,它要求我输入密码。 Now, If i enter wrong password, Bluetooth doesn't turn off, but toggle button changed its state to turn Off. 现在,如果我输入了错误的密码,则蓝牙不会关闭,但是切换按钮将其状态更改为关闭。
Here is What I tried so far:- 这是我到目前为止尝试过的:
setContentView(R.layout.activity_main);
mb = BluetoothAdapter.getDefaultAdapter();
tb1 = (ToggleButton)findViewById(R.id.appToggleBtn);
tb1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Check();
}
});
}
public void Check()
{
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
dialog.setCancelable(true);
dialog.show();
ImageView saveBtn=(ImageView)dialog.findViewById(R.id.confirmdialogBtn);
final EditText password=(EditText)dialog.findViewById(R.id.confirmdialogEditText);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if(password.getText().toString().equals("asd"))
{
if(mb.isEnabled())
{
mb.disable();
tb1.setChecked(mb.isEnabled());
}
else
{
mb.enable();
tb1.setChecked(mb.isEnabled());
}
}
else
{
Toast.makeText(getApplicationContext(), "Wrong", 1000).show();
if(mb.isEnabled())
{
tb1.setChecked(mb.isEnabled());
}
else
{
tb1.setChecked(mb.isEnabled());
}
}
dialog.dismiss();
}
});
}
Also, for Toggle Button, I used:- 另外,对于切换按钮,我使用了:-
android:background="@drawable/toggle_selector"
and in toggle_selector.xml 并在toggle_selector.xml中
<item android:drawable="@drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false"/>
Please Please Help me into this. 请帮助我。 I am badly stuck at this point. 在这一点上,我很困。 Thanks in advance. 提前致谢。
try this. 尝试这个。
if(password.getText().toString().equals("asd"))
{
if(mb.isEnabled())
{
mb.disable();
tb1.setChecked(mb.isEnabled());
}
else
{
mb.enable();
tb1.setChecked(mb.isEnabled());
}
}
else
{
Toast.makeText(getApplicationContext(), "Wrong", 1000).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.