【问题标题】:How do I detect user has clicked volume down button using key event? [duplicate]如何使用按键事件检测用户是否单击了音量减小按钮? [复制]
【发布时间】:2021-03-04 03:33:56
【问题描述】:

我写了代码来检测音量是否被按下:

public String OnKeyDown(int keycode,KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
a="Yes, It is pressed";
}else{a="No";}

return a;

对于音量降低的 android 键码是 25。当我给出 25 的值时,即使我没有按下音量,它总是返回是。

【问题讨论】:

标签: java android


【解决方案1】:

你可以简单地做到这一点。


class MainActivity : AppCompatActivity() { 
    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        setContentView(R.layout.activity_main) 
  
    } 
  
    // 1. onKeyDown is a boolean function, which returns the state of the KeyEvent. 
    // 2. This function is an internal function, that functions outside the actual application. 
    // 3. When the any Key is pressed, a Toast appears with the following message. 
    // 4. This code can be used to check if the device responds to any Key. 
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { 
  
        when (keyCode) { 
            KeyEvent.KEYCODE_VOLUME_DOWN -> Toast.makeText(applicationContext, "Volume Down Key Pressed", Toast.LENGTH_SHORT).show() 
            KeyEvent.KEYCODE_VOLUME_UP -> Toast.makeText(applicationContext, "Volume Up Key Pressed", Toast.LENGTH_SHORT).show() 
        return true
    } 
}


告诉我结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    相关资源
    最近更新 更多