【问题标题】:Android simulate button pressAndroid模拟按钮按下
【发布时间】:2011-03-08 08:29:15
【问题描述】:

我正在尝试在我的 android 应用程序中执行事件鼠标按下。当用户在文本字段中输入文本时输入回车。当在我的 OnClickListener 中为 EditText ui 检测到该字符时,我想在 ADD 按钮上执行鼠标按钮。

【问题讨论】:

    标签: android mouseevent


    【解决方案1】:

    彼得-

    听起来您想要做的是覆盖给定 EditText 的 EditorAction,然后以编程方式执行与 OnClickListener 相同的操作。例如:

    EditText inputText; //This is either created in code or inflated via XML
    Button addButton;   //This is either created in code or inflated via XML
    
    inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            addButton.performClick();
            //Tell the system you consumed the action event
            return true;
        }
    });
    

    actionId 也可以是一个有用的属性,因为它根据显示的软键盘方法报告特定操作(DONE、NEXT 等)...但请记住,如果用户从硬件键盘按下 enter该操作将始终为EditorInfo.IME_NULL,因此监控此值可能无法满足您的目的。

    与覆盖 KeyEvent 侦听器相比,这是一种更安全的方法,因为您使用不想要且不知道您窃取的事件的风险较小。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多