[英]editable EditText in clickable listView
我想用可編輯的editText做一個clickable listView。
我有一個自定義列表adapter.java,自定義列表item.xml,mainActivity.java。
我試過了
1. android:descendantFocusability =“ blocksDescendants”
=>失敗。 無法編輯editText。
2. editText android:focusable / enable / clickable = true
=>失敗。 無法單擊列表查看項目
3. getView {editText.onClickListener}
=>失敗。
我想編輯可編輯文本,而不僅僅是單擊可編輯文本和可單擊listView(listItem)。 請幫忙。
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>
...
經過我自己的幾次嘗試,我已向Google叔叔詢問了您的問題。 你知道嗎,他說了什么?
ListView無法很好地處理EditText視圖。 如果很多人以前無法解決此問題,也許您會考慮采用此處描述的 “解決方法”。 這是對其他問題的解答,但是即使您解決了上述問題,也可能會遇到此問題。
簡而言之,@ Andrew建議在內部使用帶有簡單LinearLayout的ScrollLayout而不是ListView。 在onCreate方法中,他將用於列表項的View放大並添加到LinearLayout中,並將其存儲在ArrayList中,以便以后將數據保存到每個視圖中。
我知道這不是解決您問題的方法,但是也許它可以讓您花費大量時間,您將花費大量時間來尋找任何合理的解決方案。
編輯
這很有趣。 受@Rishabh Srivastava鏈接的啟發,我試圖找到一些解決方案(我知道,我有些固執)。
我已經創建了適配器布局-RelativeLayout,該布局完全由Button填充,並且在其上方(我的意思是在Z軸上方的字面意思是在Z軸上方)已放置EditText視圖。 我以為edittext將處理對它的單擊,而按鈕將處理在edittext視圖之外的單擊。 不幸的是,“單擊”事件會在所有視圖中傳播-因此,通過單擊edittext,我們也將單擊按鈕。
我以為自己比每個人都聰明,所以我使用了OnTouchListener-我們可以處理單個“觸摸”事件並返回真實值,作為向我們處理它的信息。
你知道嗎? 我遇到了與上述鏈接中描述的問題完全相同的問題:
當我單擊EditText時,虛擬鍵盤會顯示其自身,但是EditText失去了焦點,因此我必須再次單擊EditText。
希望您不要再浪費時間了;)
首先,謝謝大家!
我嘗試了所有答案,但沒有成功... :(
ListView和onItemClick中的可聚焦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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.