简体   繁体   English

可点击列表视图中的可编辑EditText

[英]editable EditText in clickable listView

I want to make a clickable listView with editable editText. 我想用可编辑的editText做一个clickable listView。
I have a custom list adapter.java, custom list item.xml, mainActivity.java. 我有一个自定义列表adapter.java,自定义列表item.xml,mainActivity.java。

I tried 我试过了
1. android:descendantFocusability="blocksDescendants" 1. android:descendantFocusability =“ blocksDescendants”
=> failed. =>失败。 can't edit editText. 无法编辑editText。
2. editText android:focusable/enable/clickable = true 2. editText android:focusable / enable / clickable = true
=> failed. =>失败。 can't click listView Item 无法单击列表查看项目
3. getView{editText.onClickListener} 3. getView {editText.onClickListener}
=>failed. =>失败。

I want to EDITABLE editText, not just clickable editText & Clickable listView(listItem). 我想编辑可编辑文本,而不仅仅是单击可编辑文本和可单击listView(listItem)。 please help. 请帮忙。

customitem.XML customitem.XML

... ...

   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:descendantFocusability="blocksDescendants"
    >

    //android:descendantFocusability="blocksDescendants" doesn't work

    <EditText 
        android:id="@+id/tvItem"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:textColor="#ffffff"
        android:layout_marginLeft="25dp"
        android:textStyle="bold"
        android:background="@null"
        android:shadowColor="@color/shadow"
        android:shadowDx="3"
        android:shadowDy="3"
        android:shadowRadius="1"
        android:imeOptions="actionDone"
        android:inputType="text"
        android:focusable="true"
        />
    <TextView
        android:id="@+id/tvItemCount"
        android:layout_width="@dimen/list_height"
        android:layout_height="70dp"
        android:layout_alignParentRight="true"
        android:textColor="#ffffff"
        android:textSize="22dp"
        android:text="6"
        android:background="#3300b7ff"
        android:gravity="center_vertical|center_horizontal"
        android:shadowColor="@color/shadow"
        android:shadowDx="3"
        android:shadowDy="3"
        android:shadowRadius="1"
        android:textStyle="bold"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@color/dark_shadow" >

    </LinearLayout>
</RelativeLayout>

... ...

After few attempts which I've made by myself, I've asked uncle Google about your problem. 经过我自己的几次尝试,我已向Google叔叔询问了您的问题。 Do you know, what he said? 你知道吗,他说了什么?

ListView aren't able to handle EditText view well. ListView无法很好地处理EditText视图。 If many people couldn't resolve this issue before, maybe you will consider some 'workaround' like this desribed HERE . 如果很多人以前无法解决此问题,也许您会考虑采用此处描述的 “解决方法”。 It is an answer to other issue, but probably even if you will fix above problem, you will meet this one. 这是对其他问题的解答,但是即使您解决了上述问题,也可能会遇到此问题。

In brief @Andrew recommends to use ScrollLayout with simple LinearLayout inside instead of ListView. 简而言之,@ Andrew建议在内部使用带有简单LinearLayout的ScrollLayout而不是ListView。 In onCreate method he inflates the View used for list items and add it to LinearLayout, and store this in ArrayList as well, to save data to every view later. 在onCreate方法中,他将用于列表项的View放大并添加到LinearLayout中,并将其存储在ArrayList中,以便以后将数据保存到每个视图中。

I know it isn't solution for your problem, but maybe it let you a lot of time, which you will spend looking for any reasonable solution. 我知道这不是解决您问题的方法,但是也许它可以让您花费大量时间,您将花费大量时间来寻找任何合理的解决方案。

Edit 编辑

It is funny. 这很有趣。 Inspired by @Rishabh Srivastava link I've tried to find some solution (I know, I'm a little bit stubborn). 受@Rishabh Srivastava链接的启发,我试图找到一些解决方案(我知道,我有些固执)。

I've create adapter layout - RelativeLayout, which is fully filled by Button and above it (I mean literally above it - in Z axis) I've placed EditText view. 我已经创建了适配器布局-RelativeLayout,该布局完全由Button填充,并且在其上方(我的意思是在Z轴上方的字面意思是在Z轴上方)已放置EditText视图。 I thought that edittext will handle click on it and button will handle clicks outside of edittext view. 我以为edittext将处理对它的单击,而按钮将处理在edittext视图之外的单击。 Unfortunately 'click' event propagate through all of views - so by clicking on edittext, we will click on button as well. 不幸的是,“单击”事件会在所有视图中传播-因此,通过单击edittext,我们也将单击按钮。

I thought I am smarter than everybody so I used OnTouchListener - we can handle single 'touch' event and return true value, as information to OS that we handle it. 我以为自己比每个人都聪明,所以我使用了OnTouchListener-我们可以处理单个“触摸”事件并返回真实值,作为向我们处理它的信息。

And you know what? 你知道吗? I've met problem exactly the same like desribed in above link: 我遇到了与上述链接中描述的问题完全相同的问题:

When I click on an EditText, the virtual keyboard shows itself, but the EditText loses focus and I have to click the EditText again. 当我单击EditText时,虚拟键盘会显示其自身,但是EditText失去了焦点,因此我必须再次单击EditText。

I hope you don't want lost your time any more;) 希望您不要再浪费时间了;)

first of all, thank you everyone! 首先,谢谢大家!

I tried all of the answer, but it didn't work...:( 我尝试了所有答案,但没有成功... :(
Focusable EditText in the ListView and onItemClick ListView和onItemClick中的可聚焦EditText
it works for me 这个对我有用

my code ▼ 我的代码▼

public class subMyListAdapter extends BaseAdapter{
Context context;
LayoutInflater Inflater;
ArrayList<subMyItem> arraySrc;
int layout;
static int currentTheme = 0;
EditText tvItem;
RelativeLayout rl_inflate;
UserHolder holder;
public subMyListAdapter(Context context, int layout, ArrayList<subMyItem> arraySrc)
{
    this.context = context;
    this.layout = layout;
    this.arraySrc = arraySrc;
    Inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
    return arraySrc.size();
}
public String getItem(int position)
{
    return arraySrc.get(position).list;
}
public long getItemId(int position)
{
    return position;
}
public View getView(final int position, View conv, ViewGroup parent)
{
    holder = null;
     if (conv == null) 
     {
       LayoutInflater inflater = ((Activity) context).getLayoutInflater();
       conv = inflater.inflate(layout, parent, false);
       holder = new UserHolder();
       holder.tvItem = (EditText)conv.findViewById(R.id.tvItem);
       conv.setTag(holder);
     }
     else 
     {
       holder = (UserHolder) conv.getTag();
     }
     if(holder == null)
     {
       holder = new UserHolder();
       holder.tvItem = (EditText)conv.findViewById(R.id.tvItem);
       conv.setTag(holder);
     }
     subMyItem user = arraySrc.get(position);
     holder.tvItem.setOnTouchListener(test);
     conv.setOnTouchListener(test);
    if(conv == null)
    {
        conv = conv;
    }
    tvItem = (EditText) conv.findViewById(R.id.tvItem);
    user = arraySrc.get(position);
    tvItem.setText(user.list);
    tvItem.setOnClickListener(new View.OnClickListener() 
     {
       @Override
       public void onClick(View v) 
       {
           Toast.makeText(context, "tvItem button Clicked",
           Toast.LENGTH_LONG).show();
       }
    });
    return conv;
}
View.OnTouchListener test=  new View.OnTouchListener() 
{
    @Override
    public boolean onTouch(View view, MotionEvent event) 
    {
        if (view instanceof EditText) 
        {
            EditText editText = (EditText) view;
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
        } else 
        {
            UserHolder holder = (UserHolder) view.getTag();
            holder.tvItem.setFocusable(false);
            holder.tvItem.setFocusableInTouchMode(false);
        }
    return false;
    }
};
 static class UserHolder 
 {
      EditText tvItem;
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM