Don't let AdMob ads request focus?

2019-05-30 20:29发布

问题:

I have a EditText in a layout, and I also have AdMob ads at the bottom of the layout, when someone enters something in the EditText theres suppose to be a "Done" button in the button right corner of the keyboard, but after I added AdMob it has a "Next" button, and when you press it, it focuses on the Ad! Is there a way to disable this?

My XML:

<com.google.ads.AdView android:id="@+id/adView"
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adUnitId="XXXXXXXXXXX"
        ads:adSize="BANNER"
        ads:testDevices="XXXXXXXXXXX"/>

My Java:

    if (showAds == true) {
    AdView adView = (AdView)this.findViewById(R.id.adView);
    adView.loadAd(new AdRequest());
                         }

Note: I dont know if this matters, but I am using a ViewPager so the AdView is on my Main.xml layout file and the EditText is on the Layout that my ViewPager inflates.

回答1:

If you want to retain the "Done" button in your EditText, add this attribute to it: android:imeOptions="actionDone". This will force the "Done" button to appear on soft keyboards.

If you want to retain the "Next" button instead, and have it skip the AdView, you can use this method to skip over the AdView when hitting "Next".



回答2:

This does not work for me, after editText looses focus ACTION_DONE is triggered, AdMob webView gains focus and stays focused unless user touches the screen. To correct this either remove focus from current focus or close soft keyboard

etAddNote.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {           
                activity.getCurrentFocus().clearFocus();

                return true;
            }
            return false;
        }
    });

or close soft keyboard

etAddNote.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                View view = activity.getCurrentFocus();
                if (view != null) {
                    InputMethodManager imm = (InputMethodManager) activity
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                return true;
            }
            return false;
        }
    });


回答3:

Just add this tag in your root layout.

android:descendantFocusability="blocksDescendants"

Ex.

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:descendantFocusability="blocksDescendants"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        .
        .
        .
        .
        </LinearLayout>
</ScrollView>