问题
I have created custom ListView
which is having two TextViews
, Two Buttons
and one EditText
,
My problem is:
1) Edit text is loosing control even after adding android:descendantFocusability="beforeDescendants"
to root layout and beforeDescendants blocks onItemClick and onClick of buttons
2) If I add android:descendantFocusability="BlocksDescendants"
in root layout then onItemClick of list and onClick of buttons start working but EditText become untouchable even after touching edittext keyboard doesnt show up.
Can anyone please guide me?
My Code :
<?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:descendantFocusability="beforeDescendants"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_separator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/dark_grey"
android:paddingLeft="15dp"
android:text="TextView"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/lighter_gray"
android:paddingLeft="15dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_detail_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tv_detail_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:paddingBottom="5dp"
android:paddingRight="10dp"
android:paddingTop="5dp" >
<ImageButton
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="1dp"
android:src="@drawable/right_arrow" />
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/edittext_background"
android:enabled="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textPersonName" >
</EditText>
<ImageButton
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="1dp"
android:src="@drawable/left_arrow" />
</LinearLayout>
</LinearLayout>
回答1:
I suggest try to implement custom listview for this we create a adapter in which we will create two layouts
Layout1)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Layout2)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp"
android:text="TextView" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="29dp"
android:text="Button" />
</RelativeLayout>
And in .java class create a custom adapter for listview Test.java)
public class Testttt extends Activity {
ListView listView;
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.direction);
findId();
executionMethod();
}
private void findId() {
listView = (ListView) findViewById(R.id.listView1);
adapter = new CustomAdapter(this);
}
private void executionMethod() {
listView.setAdapter(adapter);
}
class CustomAdapter extends BaseAdapter {
public CustomAdapter(Context convertView) {
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
View view = inflater.inflate(R.layout.qwerty, null);
Button btn1 = (Button) view.findViewById(R.id.button1);
Button btn2 = (Button) view.findViewById(R.id.button2);
EditText edt = (EditText) view.findViewById(R.id.editText1);
return view;
}
}
}
It should be note that in customadapter class you can set functionality in particular item according to your requirement
回答2:
In your manifest file add android:windowSoftInputMode="stateHidden|adjustPan"
under activity tag in which you have made list view
来源:https://stackoverflow.com/questions/24320577/edittext-and-buttons-in-listview-android