これを行うためのより良い方法があると確信しています...しかし、私はそれを理解できませんでした。だからここに私が思いついたものがあります。
の独自の拡張機能を作成しましたButton
。そしてMyButton
、 my 内のコンポーネントを作成しますxmls
。
<com.stackoverflow.android.example.MyButton android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></com.stackoverflow.android.example.MyButton>
Button
のすべてのインスタンスをに変更する必要がありますcom.stackoverflow.android.example.MyButton
。
package com.stackoverflow.android.example;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.Button;
public class MyButton extends Button {
final int MSG_MYBUTTON_HIGHLIGHT_ON = 0;
final int MSG_MYBUTTON_HIGHLIGHT_OFF = 1;
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_MYBUTTON_HIGHLIGHT_ON:
setPressed(true);
sendEmptyMessageDelayed(MSG_MYBUTTON_HIGHLIGHT_OFF, ViewConfiguration.getPressedStateDuration());
break;
case MSG_MYBUTTON_HIGHLIGHT_OFF:
setPressed(false);
default:
break;
}
};
};
public void performVirtualClick()
{
mHandler.sendEmptyMessage(MSG_MYBUTTON_HIGHLIGHT_ON);
performClick();
}
}
関数を呼び出すperformVirtualClick
...それで十分です。