1

我有以下 XML 布局:

在此处输入图像描述

目前我只能单击 EditText 下方的“添加”按钮,即使我已经实现并覆盖了扩展 ListActivity 超类中的“OnListViewClick”方法。

我相信这是使用带有 ListView 的按钮时的焦点问题。

希望有人能告诉我如何设置 XML 布局,以便我可以同时单击按钮和 ListView。

OnItemListClick 方法代码:

package com.example.flybase2;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ShoppingList extends ListActivity implements OnClickListener {

Button AddItem;
ListView showItems;
SimpleCursorAdapter cursorAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.shoppinglistlayout);

    AddItem = (Button) findViewById(R.id.btnAddItem);

    showItems = (ListView)findViewById(android.R.id.list);

    AddItem.setOnClickListener(this);

    setList();

}

@Override
public void onClick(View clickedAdd) {


    AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this);

        builder.setTitle("Enter Item Details:");

                            LinearLayout layout = new LinearLayout(this);
                            layout.setOrientation(LinearLayout.VERTICAL);

                            final EditText titleBox = new EditText(this);

                            titleBox.setHint("Item Name:");
                            layout.addView(titleBox);

                            final EditText quantityBox = new EditText(this);

                            quantityBox.setHint("Item Quantity");
                            layout.addView(quantityBox);

                            final EditText priceBox = new EditText(this);

                            priceBox.setHint("Item Price.");
                            layout.addView(priceBox);

                            builder.setView(layout);


                            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                            try
                            {

                                Editable valueItem = titleBox.getText();
                                Editable valueAmount = quantityBox.getText();
                            Editable valuePrice = priceBox.getText();

                                String itemDescription = valueItem.toString();
                                String s = valueAmount.toString();
                                int itemAmount = Integer.parseInt(s);
                                String a = valuePrice.toString();
                                int itemPrice = Integer.parseInt(a);

                            DBHandlerShop addItem = new DBHandlerShop(ShoppingList.this, null, null);
                            addItem.open();
                            addItem.insertItems(itemDescription, itemAmount, itemPrice);
                            addItem.close();

                            }
                            catch(Exception e)
                            {
                            Dialog e1 = new Dialog(ShoppingList.this);
                            e1.setTitle("Item unsuccesfully added");
                            TextView txt = new TextView(ShoppingList.this);
                            txt.setText("Success");
                            e1.setContentView(txt);
                            e1.show();

                            }
                            finally
                            {
                                Dialog e = new Dialog(ShoppingList.this);
                            e.setTitle("Item succesfully added.");
                            TextView txt = new TextView(ShoppingList.this);
                            txt.setText("Success");
                            e.setContentView(txt);
                            e.show();

                        setList();
                            }

                              }

                            });

                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int whichButton) {

                              }
                            });

                            builder.show();

}

@Override
protected void onListItemClick(ListView l, View v, int position, long idd) {
    super.onListItemClick(l, v, position, idd);

final CharSequence[] items = {"Edit Appointment Details", "Delete Appointment"};

    Builder alertDialogBuilder = new AlertDialog.Builder(ShoppingList.this);

    alertDialogBuilder.setTitle("Appointment Options:");

    alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {

            if (items[item].equals("Edit Appointment Details")) {

            }

            else if (items[item].equals("Delete Appointment")) {

            }

            }  

        });

    alertDialogBuilder.show();

}


private void setList() {

    DBHandlerShop DBShop = new DBHandlerShop(this, null, null);

    DBHandlerShop searchItems = new DBHandlerShop(this, null, null);

    searchItems.open();

    Cursor cursor = searchItems.getItems();

    startManagingCursor(cursor);

    String [] from = new String [] {DBShop.KEY_ITEMSHOP, DBShop.KEY_ITEMNUM, DBShop.KEY_ITEMPRICE};
    int [] to = new int [] {R.id.txtSetItem, R.id.txtSetAmount, R.id.txtSetPrice};

    cursorAdapter = new SimpleCursorAdapter(this, R.layout.setshoppinglist, cursor, from, to);
    showItems.setAdapter(cursorAdapter);

}

}

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="84dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imgLink"
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/viewcon" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:gravity="center"
            android:text="Shopping List"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="35sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/txtItemDes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Item To Add To The List:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/inputAppointName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter A Name" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnAddItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
4

1 回答 1

1

在聊天中,您为 ListView 中的行发布了 XML。行布局中的一个 LinearLayouts 是可点击的(android:clickable="true"),这将消耗阻止它到达的触摸事件onListItemClick()。只需删除此行。


另外
,我建议采用稍微不同的方法。很明显,您需要一个可检查的自定义行布局(通过单击行上的任意位置)。我发布了一个详细的答案:ListView 行中的 CheckedTextView 复选标记未显示,不久前,这里有一个模糊的教程(您需要单击“Begemot”库的链接。)

于 2013-02-11T19:05:15.800 回答