当onLongClick

时间:2018-01-31 08:37:33

标签: android android-layout android-edittext autocompletetextview android-search

我在显示剪切/复制/粘贴菜单时遇到问题。在AutoCompleteTextView中编写一个字符串后,在单词开始剪切/复制/粘贴菜单的第一个选择(LongClick)中。不幸的是,这个菜单与其原始位置脱节。 This is the result when longClicked on the word. It's impossible to select any actions on it.

有趣的是,当我点击左右选择指示符来改变单词的范围时,剪切/复制/粘贴菜单会自行渲染并更正其位置。现在可以单击剪切/粘贴/复制操作按钮。 This is the result when renders itself in correct position and no cropping issue stays on the screen

我试图改变一切以找到导致此问题的原因但没有任何改变。我认为有些东西会在裁剪时触发android的默认剪切/复制/粘贴菜单。不幸的是我找不到它是什么。

任何建议表示赞赏。感谢。

这个问题可能会发生:

  • 在AndroidManifest.xml中 - >在相关活动中,“android:configChanges =”keyboard | keyboardHidden | orientation | screenLayout | uiMode | screenSize | smallestScreenSize“
  • 在AndroidManifest.xml中 - >在相关活动中,“android:theme = @ style / customTheme”
  • 在AndroidManifest.xml中 - >在相关活动中,android:windowSoftInputMode =“adjustPan”
  • 在MyDialogFragment.java中 - >同时设置actionBar

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:installLocation="auto">


    <uses-permission android:name="com.example.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />


    <application
          android:name="com.example.MainApplication"
          android:hardwareAccelerated="true"
          android:icon="@drawable/icon"
          android:label="@string/app_name"
          android:largeHeap="true"
          android:theme="@style/ExampleTheme">


        <activity
            android:name="com.example.MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:launchMode="singleTask"
            android:screenOrientation="sensorPortrait"
            android:theme="@style/ExampleTheme"
            android:windowSoftInputMode="adjustPan" />

        <activity
            android:name="com.example.SplashScreen"
            android:launchMode="singleTop"
            android:screenOrientation="sensorPortrait"
            android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>



    </application>


</manifest>

的themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="ExampleTheme" parent="Theme.AppCompat.Light">
        <item name="android:editTextStyle">@style/Widget.EditText</item>
        <item name="android:windowBackground">@color/app_bg</item>
        <item name="android:listViewStyle">@style/TransparentListView</item>
        <item name="buttonStyle">@style/ButtonStyle</item>
        <item name="android:alertDialogStyle">@style/customDialogTheme</item>
        <item name="android:alertDialogTheme">@style/customDialogTheme</item>
        <item name="android:expandableListViewStyle">@style/TransparentExpandableListView</item>
        <item name="android:spinnerStyle">@style/CustomSpinnerStyle</item>
        <item name="android:dropDownSpinnerStyle">@style/CustomSpinnerStyle</item>
    </style>

    <style name="ButtonStyle" parent="Widget.AppCompat.Button">
        <item name="android:textAllCaps">false</item>
    </style>

    <style name="Widget.EditText" parent="@android:style/Widget.EditText">
        <item name="android:background">@drawable/input_area</item>
    </style>

    <style name="TransparentListView" parent="@android:style/Widget.ListView">
        <item name="android:cacheColorHint">@color/transparent</item>
    </style>

    <style name="TransparentExpandableListView" parent="@android:style/Widget.ExpandableListView">
        <item name="android:cacheColorHint">@color/transparent</item>
    </style>

    <style name="customDialogTheme" parent="android:Theme.Holo.Light.Dialog.NoActionBar">
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:background">@color/transparent</item>
    </style>

    <style name="DialogFullScreenTheme" parent="android:Theme.Dialog">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:windowAnimationStyle">@style/BlurDialogAnimation</item>

        <!-- No backgrounds, titles or window float -->
        <item name="android:windowBackground">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
    </style>
    <style name="BlurDialogAnimation">
        <item name="android:windowEnterAnimation">@anim/fadein</item>
        <item name="android:windowExitAnimation">@anim/fadeout</item>
    </style>

    <style name="DialogFullScreenThemeHolo" parent="android:Theme.Holo.Light.Dialog">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:windowAnimationStyle">@style/BlurDialogAnimation</item>

        <!-- No backgrounds, titles or window float -->
        <item name="android:gravity">center</item>
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
    </style>

    <!-- Overwrite the ToggleButton style -->
    <style name="Widget.Button.Toggle" parent="android:Widget">
        <item name="android:background">@drawable/btn_toggle_bg</item>
        <item name="android:textOn">ON</item>
        <item name="android:textOff">OFF</item>
        <item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
    </style>

</resources>

MyDialogFragment.java:

public class MyDialogFragment extends DialogFragment{
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog d = new Dialog(getActivityInstance(), R.style.DialogFullScreenTheme);
        d.setContentView(R.layout.search_dialog_fragment);
        actionSearchBarET = (AutoCompleteTextView) d.findViewById(R.id.actionSearchBoxACT);
        actionBar = (RelativeLayout) d.findViewById(R.id.actionBar);
        setupSearchBar();
        actionSearchBarET.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
            InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
            } catch (Exception ignored) {}
        }
        });
        actionSearchBarET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionSearchBarET != null && actionSearchBarET.getText() != null && !actionSearchBarET.getText().toString().equalsIgnoreCase("")) {
                    searchWithKeyword(actionSearchBarET.getText().toString());
                }
                return true;
            }
        });
    }

    private void setupSearchBar() {
        TypedValue tv = new TypedValue();
        RelativeLayout.LayoutParams actionBarParams;
        if(getActivityInstance().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            int actionBarHeight = TypedValue.complexToDimensionPixelOffset(tv.data,     getActivityInstance().getResources().getDisplayMetrics());
            if(actionBarHeight > 0) {
                actionBarParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,  actionBarHeight);
            } else {
                actionBarParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,  getActivityInstance().getResources().getDimensionPixelOffset(   R.dimen.action_bar_default_height));
            }
        } else {
            actionBarParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,  getActivityInstance().getResources().getDimensionPixelOffset(R.dimen.action_bar_default_height));
        }
        actionBar.setMinimumHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,     getResources().getDimension(R.dimen.action_bar_default_height), getResources().getDisplayMetrics()));
        actionBar.setLayoutParams(actionBarParams);

    }
}

search_dialog_fragment.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:paddingBottom="15dp">

    <include
            android:id="@+id/actionBar"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/ab_search_and_textbutton" />

</RelativeLayout>

ab_search_and_textbutton.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:paddingLeft="15dp">

    <!-- Dummy item to prevent EditText from receiving focus -->
    <LinearLayout
        android:id="@+id/dummy"
        android:layout_width="0dip"
        android:layout_height="0dip"
        android:focusable="true"
        android:layout_alignParentRight="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical"/>

    <RelativeLayout
        android:id="@+id/searchBarContainer"
        android:layout_centerVertical="true"
        android:minHeight="@dimen/searchbar_height"
        android:background="@drawable/search_box_bg"
        android:layout_gravity="center"
        android:layout_toLeftOf="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/searchbar_height">

        <ImageView
            android:id="@+id/searchIcon"
            android:layout_marginLeft="15dp"
            android:layout_centerVertical="true"
            android:src="@drawable/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <AutoCompleteTextView
            android:layout_toRightOf="@+id/searchIcon"
            android:layout_marginLeft="3dp"
            android:id="@+id/actionSearchBoxACT"
            android:textSize="14dp"
            android:inputType="textNoSuggestions"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/search"
            android:focusableInTouchMode="true"
            android:focusable="true"
            android:textColor="@color/textcolor_black"
            android:layout_centerInParent="true"
            android:background="@null"
            android:completionThreshold="1"
            android:imeOptions="actionSearch"
            android:nextFocusLeft="@id/actionSearchBoxACT"
            android:nextFocusUp="@id/actionSearchBoxACT"
            android:paddingLeft="5dp"
            android:singleLine="true">
            <requestFocus />
        </AutoCompleteTextView>


    </RelativeLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

剪切/粘贴/复制菜单可能会受到主题xml的影响。 DialogFullScreenTheme样式包含重力场。您可以删除该字段,但必须检查所有对话框ui。

<style name="DialogFullScreenTheme" parent="android:Theme.Dialog">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:windowAnimationStyle">@style/BlurDialogAnimation</item>
        <item name="android:windowBackground">@null</item>

        <!-- Remove this line -->
        <item name="android:gravity">center</item>

        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
    </style>