Seb*_*ian 15 android google-tv
你好,
我已经构建了一个自定义视图,RelativeLayout使用它作为自定义图像按钮.到目前为止,选择它有效(PIC2),甚至当我点击它(使用GoogleTV Remote)时,视图成功地将其状态更改为PIC3(由于android:duplicateParentState="true")
但不幸的是,onClickListener它不会触发(如果我点击带遥控器的视图"OK"按钮或者我使用触摸板也没关系..)
我真的需要像普通按钮一样的行为.怎么做到这一点?我已经花了几个小时的搜索谷歌和StackOverflow的...(顺便说一句,当设置android:clickable="false"为RelativeLayout,在OnClickListener工作,但只有当我使用鼠标指针(触摸板),之后焦点将丢失,状态(如图3)没有显示)



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="false">
<TextView
android:id="@+id/caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:background="@drawable/btn_rounded_corners"
android:paddingLeft="25dp"
android:textSize="15sp"
android:duplicateParentState="true"/>
<ImageView
android:id="@+id/icon"
style="@style/Menu_Button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="-50dp"
android:layout_toLeftOf="@id/caption"
android:background="@drawable/btn_main_menu_back_shape"
tools:ignore="ContentDescription"
android:duplicateParentState="true" />
Run Code Online (Sandbox Code Playgroud)
public class RoundedButton extends RelativeLayout {
private String label;
private int icon;
/**
* @param context
*/
public RoundedButton(Context context)
{
super(context);
initAttributes(context, null);
}
/**
* @param context
* @param attrs
*/
public RoundedButton(Context context, AttributeSet attrs)
{
super(context, attrs);
initAttributes(context, attrs);
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public RoundedButton(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initAttributes(context, attrs);
}
private void initAttributes(Context context, AttributeSet attrs)
{
LayoutInflater.from(context).inflate(R.layout.rounded_button, this, true);
TypedArray a =
context.obtainStyledAttributes(attrs, R.styleable.RoundedButton);
final int N = a.getIndexCount();
for (int i = 0; i < N; ++i)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.RoundedButton_text:
setLabel(a.getString(attr));
break;
case R.styleable.RoundedButton_icon:
setIcon(a.getResourceId(attr, 0));
break;
}
}
a.recycle();
}
public String getLabel()
{
return this.label;
}
public void setLabel(final String label)
{
this.label = label;
((TextView)findViewById(R.id.caption)).setText(this.label);
}
/**
* @return the icon
*/
public int getIcon()
{
return icon;
}
/**
* @param icon the icon to set
*/
public void setIcon(int icon)
{
this.icon = icon;
((ImageView)findViewById(R.id.icon)).setImageResource(this.icon);
}
}
Run Code Online (Sandbox Code Playgroud)
<eu.test.custom_views.RoundedButton
android:id="@+id/custombutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:icon="@drawable/hand_icon_green_left"
custom:text="Normal state" />
Run Code Online (Sandbox Code Playgroud)
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((RoundedButton) findViewById(R.id.custombutton)).setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.custombutton) { Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show(); }
}
}
Run Code Online (Sandbox Code Playgroud)
Seb*_*ian 28
我现在明白了......解决方案很简单,需要一些时间;-)
dispatchKeyEvent(KeyEvent event)在RoundedButton.java中重写并实现自己的OnClickListener.然后写一个公共setOnClickListener函数......
private OnClickListener listener;
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(listener != null) listener.onClick(this);
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
if(listener != null) listener.onClick(this);
}
return super.dispatchKeyEvent(event);
}
public void setOnClickListener(OnClickListener listener) {
this.listener = listener;
}
Run Code Online (Sandbox Code Playgroud)
public class RoundedButton extends RelativeLayout
{
private OnClickListener listener;
private String label;
private int icon;
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(listener != null) listener.onClick(this);
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
if(listener != null) listener.onClick(this);
}
return super.dispatchKeyEvent(event);
}
public void setOnClickListener(OnClickListener listener) {
this.listener = listener;
}
/**
* @param context
*/
public RoundedButton(Context context)
{
super(context);
initAttributes(context, null);
}
/**
* @param context
* @param attrs
*/
public RoundedButton(Context context, AttributeSet attrs)
{
super(context, attrs);
initAttributes(context, attrs);
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public RoundedButton(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
this.setClickable(true);
this.setEnabled(true);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
initAttributes(context, attrs);
}
private void initAttributes(Context context, AttributeSet attrs)
{
LayoutInflater.from(context).inflate(R.layout.rounded_button, this, true);
TypedArray a =
context.obtainStyledAttributes(attrs, R.styleable.RoundedButton);
final int N = a.getIndexCount();
for (int i = 0; i < N; ++i)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.RoundedButton_text:
setLabel(a.getString(attr));
break;
case R.styleable.RoundedButton_icon:
setIcon(a.getResourceId(attr, 0));
break;
}
}
a.recycle();
}
public String getLabel()
{
return this.label;
}
public void setLabel(final String label)
{
this.label = label;
((TextView)findViewById(R.id.caption)).setText(this.label);
}
/**
* @return the icon
*/
public int getIcon()
{
return icon;
}
/**
* @param icon the icon to set
*/
public void setIcon(int icon)
{
this.icon = icon;
((ImageView)findViewById(R.id.icon)).setImageResource(this.icon);
}
Run Code Online (Sandbox Code Playgroud)
}
我创建我的自定义视图的情况下(从它继承RelativeLayout的)RecyclerView.Adapter和充气list_item.xml到每一个新的实例,然后附上我的听众的onBindViewHolder()方法,但即使在理论上我正在做的事情以正确的方式,直到我删除它没有工作(我的自定义视图)的根布局中的android:clickable="true"属性list_item.xml。
| 归档时间: |
|
| 查看次数: |
11859 次 |
| 最近记录: |