使edittext光标在焦点上可见

时间:2016-03-24 11:42:44

标签: android android-fragments android-edittext android-framelayout

我的edittext片段cursorfragment layout开始时visible不可见。当您开始输入任何内容时,即cursor成为visiblecursor变为visible。我只要触摸/点击edittext,就希望onFocusChangeListener成为visible。我尝试了Fragment,但它只在layout上启用了游标framelayout。另外,我想在layout触摸/点击时显示不同的edittext(仅更改<?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"> <EditText android:id="@+id/search_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:background="@drawable/edittext_back" android:drawableLeft="@drawable/ic_search" android:drawablePadding="10dp" android:hint="@string/search_hint" android:windowSoftInputMode="stateAlwaysHidden" android:padding="10dp"/> <TextView android:id="@+id/search_filter_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/activity_vertical_margin" android:layout_below="@+id/search_et" android:text="@string/search_filter_text"/> <RadioGroup android:id="@+id/RG" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="@dimen/activity_vertical_margin" android:layout_below="@+id/search_filter_text"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/weight" android:background="@drawable/button_back" android:layout_margin="10dp"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/cycle" android:background="@drawable/button_back" android:layout_margin="10dp"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/swim" android:background="@drawable/button_back" android:layout_margin="10dp"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/run" android:background="@drawable/button_back" android:layout_margin="10dp"/> </RadioGroup> <FrameLayout android:id="@+id/framelayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/RG"> <GridView android:id="@+id/grid_view" android:layout_width="match_parent" android:layout_height="match_parent" android:horizontalSpacing="1dp" android:verticalSpacing="1dp" android:stretchMode="columnWidth" android:numColumns="3" android:scrollbars="none"/> </FrameLayout> </RelativeLayout> 而不是整个public class SearchName extends Fragment{ private GridView mGridView; private GridItem newItem; private GridAdapter mGridAdapter; private ArrayList<GridItem> mGridData; public static final String KEY_USERID = "user_id", KEY_NAME = "search_text"; private static final String FETCH_ALL_MEMBERS = "http://188.166.189.163:8001/api/member/get-all/"; private EditText editText; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.searchname, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mGridView = (GridView)view.findViewById(R.id.grid_view); displayLog(); mGridData = new ArrayList<>(); mGridAdapter = new GridAdapter(getContext(), R.layout.gridrow, mGridData); mGridView.setAdapter(mGridAdapter); editText = (EditText)view.findViewById(R.id.search_et); editText.setCursorVisible(false); editText.addTextChangedListener(editTextWatcher); } private void displayLog() { StringRequest stringRequest = new StringRequest(Request.Method.POST, FETCH_ALL_MEMBERS, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jObj = new JSONObject(response); System.out.println(jObj); String status = jObj.getString("status"); // Now check status value if (status.equals("0")) { Toast.makeText(getActivity(), "There was some error! Please try again.", Toast.LENGTH_LONG).show(); } else if (status.equals("1")) { JSONArray result = jObj.getJSONArray("result"); for (int i = 0; i < result.length(); i++) { JSONObject json_data = result.getJSONObject(i); String name = json_data.getString("name"); String user_id = json_data.getString("user_id"); newItem = new GridItem(); newItem.setName(name); newItem.setUserId(user_id); if(json_data.has("profile_picture")){ newItem.setImage(json_data.getString("profile_picture")); }else{ Uri path = Uri.parse("android.resource://com.sam.fitlincsearch/" + R.drawable.default_profile); String image_def = path.toString(); newItem.setImage(image_def); } mGridData.add(newItem); } mGridAdapter.notifyDataSetChanged(); } else { // Error in login. Get the error message String errorMsg = jObj.getString("error_msg"); Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_LONG).show(); } } catch (JSONException e) { // JSON error e.printStackTrace(); Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put(KEY_USERID, "1"); //get user_id from SQLite database. return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); requestQueue.add(stringRequest); } private final TextWatcher editTextWatcher = new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { editText.setCursorVisible(true); } public void onTextChanged(CharSequence s, int start, int before, int count) { } public void afterTextChanged(Editable s) { if (s.length() == 0) { Toast.makeText(getActivity(), "Blank Field", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getActivity(), "Entered text: " + editText.getText(), Toast.LENGTH_LONG).show(); } } }; } )。有没有办法在没有片段交易的情况下实现这个目标?

{{1}}

片段:

{{1}}

1 个答案:

答案 0 :(得分:0)

我在xml中的edittext中添加了android:focusableInTouchMode="false",并使用了setOnClickListener而不是TextWatcher。

相关问题