问题是编辑文本的焦点没有键盘打开而无法编辑......
If i Place <requestfocus /> in xml then the code is crashed.
请尽快解决我的问题。 此搜索栏与Gmail搜索栏类似。
MainActivity.XML代码
<FrameLayout
android:id="@+id/rellayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_alignTop="@+id/search"
android:layout_toLeftOf="@+id/search" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search"
android:descendantFocusability="afterDescendants" />
<Spinner android:id="@+id/dropspinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:descendantFocusability="afterDescendants"
style="@android:style/Widget.DeviceDefault.EditText" />
<ImageView
android:id="@+id/dropdownimage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/dropdown"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dp" />
</FrameLayout>
DropDown.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:background="#f0f0f0f0"
android:orientation="vertical"
android:padding="10dp" >
<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="Crop:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000" />
<EditText
android:id="@+id/crop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="XYZ"
android:layout_marginTop="2dp"
android:background="#FFF"
android:padding="3dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000"
android:layout_alignLeft="@+id/crop"
android:layout_below="@+id/crop"
android:text="Posted By:"
android:layout_marginTop="2dp" />
<EditText
android:id="@+id/postedby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:ems="10"
android:hint="XYZ"
android:layout_marginTop="2dp"
android:background="#FFF"
android:padding="3dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000"
android:layout_alignLeft="@+id/postedby"
android:layout_below="@+id/postedby"
android:text="Location"
android:layout_marginTop="2dp" />
<EditText
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:ems="10"
android:hint="Example Agra"
android:layout_marginTop="2dp"
android:background="#FFF"
android:padding="3dp" />
<Button
android:id="@+id/searchdropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/location"
android:layout_below="@+id/location"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="Search"
android:textColor="#FFF"
android:textStyle="bold"
android:background="@drawable/rounded_shape_button_green" />
</LinearLayout>
Product.java
import java.util.ArrayList;
import java.util.List;
import com.aksha.adapters.DropDownAutoComplete;
import com.aksha.adapters.LazyAdapter;
import com.aksha.adapters.MyObject;
import com.aksha.adapters.RefreshableListView;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.Spinner;
public class Product extends Fragment {
private RefreshableListView mListView;
private ArrayList<String> stringAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.product,container, false);
Typeface hindi = Typeface.createFromAsset(getActivity().getAssets(), "fonts/DroidHindi.ttf");
stringAdapter = new ArrayList<String>();
stringAdapter.add("abcde");
stringAdapter.add("abcdef");
stringAdapter.add("abcdefg");
final ImageView search = (ImageView) rootView.findViewById(R.id.search);
final ImageView dropdownimage = (ImageView) rootView.findViewById(R.id.dropdownimage);
final Spinner drop = (Spinner) rootView.findViewById(R.id.dropspinner);
drop.setClickable(false);
final AutoCompleteTextView actv = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);
actv.setThreshold(1);
mListView = ((RefreshableListView)rootView.findViewById(R.id.today_refreshable_list));
dropdownimage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
drop.performClick();
actv.clearFocus();
}
});
ArrayList<MyObject> object = new ArrayList<MyObject>();
object.add(new MyObject("ABC"));
drop.setAdapter(null);
drop.setAdapter(new DropDownAutoComplete(getActivity(), R.layout.dropdown, object, mListView, drop));
if(stringAdapter.size()>0){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, stringAdapter);
actv.setAdapter(adapter);
}
actv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
search.performClick();
}
});
mListView.setOnRefreshListener(new RefreshableListView.OnRefreshListener()
{
public void onRefresh(RefreshableListView paramAnonymousRefreshableListView)
{
try{
stringAdapter.add("abcdefg");
LazyAdapter adapter = new LazyAdapter(getActivity(), stringAdapter);
mListView.setAdapter(adapter);
mListView.completeRefreshing();
}catch(Exception e){
e.getMessage();
}
}
});
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
LazyAdapter adapter = new LazyAdapter(getActivity(), stringAdapter);
mListView.setAdapter(adapter);
}catch(Exception e){
e.getMessage();
}
}
});
return rootView;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
// TODO Auto-generated method stub
super.setUserVisibleHint(isVisibleToUser);
try{
if(isVisibleToUser){
//Code
}
}catch(Exception e){
e.getMessage();
}
}
}
DropDownAutoComplete.java