gpt4 book ai didi

java - 停止窃取 ListView 焦点的按钮

转载 作者:行者123 更新时间:2023-11-30 03:47:06 25 4
gpt4 key购买 nike

我有以下 XML 布局:

enter image description here

目前我只能单击 EditText 下方的“添加”按钮,即使我已经实现并覆盖了扩展 ListActivity 父类(super class)中的“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>

最佳答案

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


添加
我建议采用稍微不同的方法。很明显,您想要一个可检查的自定义行布局(通过单击行上的任意位置)。我发布了一个详细的答案:CheckedTextView checkmark in ListView row not showing up ,前阵子有一个vague tutorial在这里(您需要单击“Begemot”库的链接。)

关于java - 停止窃取 ListView 焦点的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14818424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com