Сделать курсор редактирования текста видимым на фокусе

У меня есть edittext во фрагменте которого cursor невидим когда fragmentlayout начинается. Это становится visible когда вы начинаете печатать что-либо, т.е. при первом нажатии клавиши, cursor становится visible, Я хочу cursor становиться 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 ответ

Я добавил android:focusableInTouchMode="false" к моему тексту редактирования в xml и использовал setOnClickListener вместо TextWatcher.

Другие вопросы по тегам