ImageButton专注但不可点击

时间:2017-03-09 12:41:55

标签: java android

我有一个对话框弹出窗口,列出所有应用并通过此

启动它们
private void addClickListener(){
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override``
            public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
                Intent i = manager.getLaunchIntentForPackage(apps.get(pos).name.toString());
                AllApps.this.startActivity`(i);
            }
        });

如果我放入布局xml,ImageButtons可聚焦,它们会突出显示,但不可点击

<ImageView
    android:id="@+id/item_app_icon"
    android:layout_width="250dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:gravity="right"
    android:background="@drawable/border_image"
    android:focusable="true"/>

我尝试添加android:clickable="true"但没有结果。

我的选择器xml(border_image)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item

    android:state_focused="false">
    <shape
        android:innerRadiusRatio="1"
        android:shape="rectangle" >
        <solid android:color="@android:color/white" />
        <corners
            android:bottomLeftRadius="5dp"
            android:bottomRightRadius="5dp"
            android:radius="5dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"></corners>
        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

    </shape></item>

<!-- button focused -->
<item
    android:state_focused="true">
    <shape
        android:innerRadiusRatio="1"
        android:shape="rectangle" >
        <solid android:color="@android:color/holo_orange_dark" />
        <corners
            android:bottomLeftRadius="5dp"
            android:bottomRightRadius="5dp"
            android:radius="5dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"></corners>
        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

    </shape></item>

弹出式对话框中的imageButtons和文本的Xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/item_app_icon"
    android:layout_width="250dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:gravity="right"
    android:background="@drawable/border_image"
    android:focusable="true"/>
<TextView
    android:id="@+id/item_app_label"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/item_app_icon"
    android:paddingTop="10dp"
    android:background="#26F0F8FF"
    android:textColor="@android:color/black"
    android:textSize="15dp"
    android:gravity="center"/>

这是对话框弹出代码

protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_apps);
    loadApps();
    loadListView();
    addClickListener();
    //onfocus();

    adpter = new dbAdapter(this);
    adpter.open();


    //addLongClickListener();
}

private void loadApps() {
    manager = getPackageManager();
    apps = new ArrayList<AppDetail>();

    Intent i = new Intent(Intent.ACTION_MAIN, null);
    i.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0);

    for (ResolveInfo ri : availableActivities) {
        AppDetail app = new AppDetail();
        app.label = ri.loadLabel(manager);
        app.name = ri.activityInfo.packageName;
        app.icon = ri.activityInfo.loadIcon(manager);
        //String name= app.name.toString();
        //String icon = app.icon.toString();
        //adpter.createApp(name,icon);
        if (app.name.equals("com.bux.launcher")){

        }
        else {
            apps.add(app);
        }
    }
}

private void loadListView() {
    list = (GridView) findViewById(R.id.apps_list);

    ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(this, R.layout.list_item, apps) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ImageButton appIcon = (ImageButton) convertView.findViewById(R.id.item_app_icon);
            appIcon.setImageDrawable(apps.get(position).icon);

            appIcon.setFocusable(true);
            appIcon.setFocusableInTouchMode(true);
            appIcon.setClickable(true);




            TextView appLabel = (TextView) convertView.findViewById(R.id.item_app_label);
            appLabel.setText(apps.get(position).label);



            return convertView;
        }
    };



    registerForContextMenu(list);
    list.setAdapter(adapter);
}

private void addClickListener(){
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> av, View v, int pos,
                                long id) {
            Intent i = manager.getLaunchIntentForPackage(apps.get(pos).name.toString());
            AllApps.this.startActivity(i);
        }
    });

}

3 个答案:

答案 0 :(得分:1)

您需要在drawable中创建XML文件,然后将以下代码粘贴到选择器和其他图像

<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item
        android:state_pressed="true"

        android:drawable="@drawable/search_sel" />
    <item
        android:drawable="@drawable/search" />
</selector>

答案 1 :(得分:0)

添加android:descendantFocusability =&#34; blocksDescendants&#34;到你的根视图

答案 2 :(得分:0)

我在你的问题中注意到你指的是ImageButtons,但是你正在使用ImageView。尝试将<ImageView>更改为<ImageButton>