Android 如何在单击编辑文本时播放声音

Android 如何在单击编辑文本时播放声音,android,android-layout,material-design,Android,Android Layout,Material Design,我想在onclick eddittext上播放声音。但我面临的问题是在单键文本上播放声音。它从第二次单击开始工作。我不想从edittext中删除焦点。请帮帮我 <LinearLayout android:id="@+id/res3" android:layout_height="wrap_content" android:orientation="horizontal" > <android.support.design.widget.TextIn

我想在onclick eddittext上播放声音。但我面临的问题是在单键文本上播放声音。它从第二次单击开始工作。我不想从edittext中删除焦点。请帮帮我

 <LinearLayout
    android:id="@+id/res3"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

 <android.support.design.widget.TextInputLayout
    android:id="@+id/input_layout_mail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

 <EditText
     android:id="@+id/mail"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:textSize="20sp"
     android:maxLength="50"
     android:inputType="textEmailAddress"
     android:hint="@string/mailid" />
 </android.support.design.widget.TextInputLayout>
 </LinearLayout>

您可以使用OnFocusChangeListener作为句柄单击EditeText:

 emailid.setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            playSound("click.mp3");
        }
    }
});

TextInputLayout
是否正在使用第一次单击?尝试将侦听器放在
TextInputLayout
EditText
上。就个人而言,我也会使用
OnClickListener
,而不是
OnTouchListener

在EditText中使用android:focusable=“false”如果我移除焦点,则android.support.design.widget.TextInputLayout效果不起作用。所以我不想从edittext中删除焦点。我也尝试过使用onTouchListener,但没有用。你可以发布你的onTouch事件代码吗?你是否尝试过
setOnFocusChangeListener
?我尝试过使用OnFocusChangeListener,但仍然不起作用。你能否提供更多详细信息,说明为什么建议使用上面的OnClickListener而不是OnTouchListener.@AJW在单击时指定的问题,因此使用它而不是onTouch是有意义的。如果你正在使用onTouch,它会触发各种各样的触摸事件,请查看文档中的完整列表(),因此如果你只需要简单的点击一下,那么使用它就更简单了。明白了,我感谢你的后续行动。干杯
 emailid.setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            playSound("click.mp3");
        }
    }
});