Java 可单击列表视图中的可编辑编辑文本

Java 可单击列表视图中的可编辑编辑文本,java,android,listview,android-listview,android-edittext,Java,Android,Listview,Android Listview,Android Edittext,我想制作一个带有可编辑编辑文本的可点击列表视图。 我有一个custom list adapter.java、custom list item.xml、mainActivity.java。 我试过了 1.android:genderantfocusability=“blocksDescendants” =>失败。无法编辑编辑文本。 2.editText android:focusable/enable/clickable=true =>失败。无法单击listView项目 3.getView{edi

我想制作一个带有可编辑编辑文本的可点击列表视图。
我有一个custom list adapter.java、custom list item.xml、mainActivity.java。

我试过了
1.android:genderantfocusability=“blocksDescendants”
=>失败。无法编辑编辑文本。
2.editText android:focusable/enable/clickable=true
=>失败。无法单击listView项目
3.getView{editText.onClickListener}
=>失败

我想要可编辑编辑文本,而不仅仅是可点击编辑文本和可点击列表视图(listItem)。 请帮忙

customitem.XML


//安卓:genderantfocusability=“blocksDescendants”不起作用

在我自己做了几次尝试之后,我向谷歌叔叔询问了你的问题。你知道他说了什么吗

ListView无法很好地处理EditText视图。如果很多人以前无法解决这个问题,也许你会考虑一些像这样的“解决办法”。这是另一个问题的答案,但即使您解决了上述问题,也可能会遇到这个问题

简言之,@Andrew建议使用内部带有简单LinearLayout的ScrollLayout,而不是ListView。在onCreate方法中,他将用于列表项的视图膨胀,并将其添加到LinearLayout,并将其存储在ArrayList中,以便以后将数据保存到每个视图

我知道这不是解决你问题的办法,但也许这会让你有很多时间,你会花在寻找任何合理的解决办法上

编辑

这很有趣。受@Rishabh Srivastava链接的启发,我试图找到一些解决方案(我知道,我有点固执)

我已经创建了AdapterLayout-RelativeLayout,它完全由按钮填充,并且在它上面(我的意思是在它上面-在Z轴上)我已经放置了EditText视图。我认为edittext将处理点击,而按钮将处理edittext视图之外的点击。不幸的是,“单击”事件会在所有视图中传播-所以通过单击edittext,我们也会单击按钮

我认为我比任何人都聪明,所以我使用了OnTouchListener——我们可以处理单个“触摸”事件并返回真实的值,作为我们处理它的操作系统的信息

你知道吗?我遇到的问题与上面链接中描述的完全相同:

当我单击EditText时,虚拟键盘会自动显示,但EditText会失去焦点,我必须再次单击EditText


我希望你不要再浪费时间了;)

首先,谢谢大家

我尝试了所有的答案,但都没用……:(

它对我有用

我的代码▼

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;
     }
 }
公共类subMyListAdapter扩展了BaseAdapter{
语境;
充气机;
ArrayList arraySrc;
int布局;
静态int currentTheme=0;
编辑文本项目;
相对膨胀;
用户持有者;
公共子MyListAdapter(上下文上下文、int布局、ArrayList arraySrc)
{
this.context=上下文;
this.layout=布局;
this.arraySrc=arraySrc;
充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
}
public int getCount()
{
返回arraySrc.size();
}
公共字符串getItem(int位置)
{
返回arraySrc.get(position.list);
}
公共长getItemId(int位置)
{
返回位置;
}
公共视图getView(最终整数位置、视图转换、视图组父级)
{
holder=null;
如果(conv==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
conv=充气机。充气(布局、父级、假);
holder=新用户holder();
holder.tviitem=(EditText)conv.findViewById(R.id.tviitem);
conv.setTag(支架);
}
其他的
{
holder=(UserHolder)conv.getTag();
}
if(holder==null)
{
holder=新用户holder();
holder.tviitem=(EditText)conv.findViewById(R.id.tviitem);
conv.setTag(支架);
}
subMyItem user=arraySrc.get(位置);
holder.tvItem.setOnTouchListener(测试);
conv.setOnTouchListener(测试);
如果(conv==null)
{
conv=conv;
}
TviItem=(EditText)conv.findViewById(R.id.TviItem);
user=arraySrc.get(位置);
tviitem.setText(user.list);
TviItem.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
Toast.makeText(上下文,“单击tvItem按钮”,
Toast.LENGTH_LONG).show();
}
});
返回conv;
}
View.OnTouchListener测试=新建View.OnTouchListener()
{
@凌驾
公共布尔onTouch(视图、运动事件)
{
如果(查看EditText的实例)
{
EditText EditText=(EditText)视图;
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
}否则
{
UserHolder=(UserHolder)view.getTag();
holder.tvItem.setFocusable(假);
holder.tvItem.setFocusableInTouchMode(false);
}
返回false;
}
};
静态类用户持有者
{
编辑文本项目;
}
}

android:DegenantFocusability=“BeforeSandrogents”如何?检查适配器上的方法“isEnabled(position)”。该方法应返回true。这可能会让您了解如何实现所需的功能。此处实现了类似的功能,但方式略有不同。单击项时,EditText会显示:
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;
     }
 }