[英]How to create buttons like keyboard keys in android?
這個問題似乎微不足道。 我想為我的應用創建一個像鍵盤鍵的按鈕。 當我點擊它時,一個彈出窗口出現在該按鈕上方,顯示按下的字母。 除了一件事以外,一切都很好。 當我將onFocusChangedListener添加到按鈕時,沒有任何反應。 我需要讓我的按鈕充當鍵盤鍵,但我不知道如何。
正如您在此處所看到的,當按鈕聚焦時,會出現一個彈出窗口。 我想這樣做,但onFocusChangeListener
不起作用。 我知道我可以使用KeyboardView
來實現這一點,但我不想使用它,因為一些其他問題,如居中按鈕和設置鍵的高度與layout_weight。 所以我需要使用普通按鈕。
我的第一次嘗試:
button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight());
} else {
popupWindow.dismiss();
}
}
});
結果:什么都沒發生。 彈出窗口根本沒有出現。
編輯:我添加了button.setFocusableInTouchMode(true);
正如阿什利建議的那樣,onFocusChanged現在正在被召喚,但它的行為非常奇怪。 彈出窗口有時會顯示,但在顯示時,它永遠不會消失......
我的第二次嘗試:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight());
break;
case MotionEvent.ACTION_UP:
popupWindow.dismiss();
break;
}
return true;
}
});
結果:這個行為很奇怪。 有時彈出窗口顯示,有時不顯示,但是當顯示時,按鈕也不會改變其狀態。 應該已經集中注意力,但按鈕沒有任何反應,它就像是處於正常狀態一樣(Button的背景不會隨着我的drawable xml中聲明的state_focused而改變)。 似乎onTouchListener會覆蓋按鈕的功能。
這是我布局的一部分:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Q"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="W"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="E"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="R"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="T"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Y"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="U"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="I"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="O"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="P"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.5" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="A"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="S"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="D"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="F"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="G"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="H"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="J"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="K"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="L"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.5" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1.5" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Z"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="X"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="C"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="V"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="B"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="N"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="M"
android:background="@drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1.5" />
</LinearLayout>
</LinearLayout>
在代碼中:
public void onKeyboardClick(View view) {
//The view pressed is a button.
final Button button = (Button) view;
//Create a PopupWindow.
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View keyboardPopup = inflater.inflate(R.layout.keyboard_popup, null);
final PopupWindow popupWindow = new PopupWindow(keyboardPopup, view.getWidth() + 20, view.getHeight());
TextView keyboardKey = (TextView) keyboardPopup.findViewById(R.id.keyboard_key);
keyboardKey.setText(button.getText().toString());
//Get button location to show the popup above it.
int[] keyLocation = new int[2];
button.getLocationOnScreen(keyLocation);
final Rect location = new Rect();
location.left = keyLocation[0];
location.top = keyLocation[1];
location.right = location.left + button.getWidth();
location.bottom = location.top + button.getHeight();
//This is a temporary solution. I don't want to use that.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Show popup.
popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Dismiss popup.
popupWindow.dismiss();
}
}, 200);
}
});
}
任何幫助將不勝感激。 謝謝。
我建議你使用第二次嘗試onTouchListener
! 你有2個問題:
1.按鈕不會改變狀態
實際上,當您覆蓋onTouchListener
您必須自己模擬狀態。 請看看這個SO線程是如何完成的: Android上的“按住”按鈕需要使用onTouchListener更改狀態(自定義XML選擇器)
2.有時顯示,有時不顯示
這不應該發生,特別是如果您正確處理所有相關的觸摸事件案例。 當用戶觸摸按鈕時,您將希望顯示彈出窗口,並在用戶移出按鈕時隱藏彈出窗口(通過向外滑動或將手指從屏幕上移開)。
請嘗試以下代碼示例:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
v.setPressed(true);
showPopupWindow(...);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
v.setPressed(false);
hidePopupWindow(...);
break;
}
return true;
}
});
getAction
來更好地處理多點觸摸。 v.setPressed(...);
- 這將更新按鈕狀態。 我沒有在你的代碼中注意到它,但可能你遺漏了。
btn.setFocusableInTouchMode(true);
btn.setFocusable(true);
對於焦點改變監聽器。
holder.sAttedenceList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (!parent.hasFocus()) {
return;
}
不是完整的代碼,而是這對我有用的一般結構。
而不是設置OnTouchListener
,嘗試OnTouchListener
Button
並重寫onTouchEvent
本身:
public class KeyboardButton extends Button
{
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight());
break;
case MotionEvent.ACTION_UP:
popupWindow.dismiss();
break;
}
return super.onTouchEvent(event);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.