Android,Admob / Adview从edittext窃取焦点,如何预防?

时间:2013-02-03 09:53:30

标签: android focus admob setfocus adview

我正在创建视图并将这些视图添加到linearlayout中。这些输入来自屏幕底部的edittext,就像一个消息应用程序。每当我按下“完成”按钮时,它会触发并将该消息添加到该消息linearlayout。

问题:

当我想在这些消息之间加上adview时,例如每10条消息之间。 Edittext失去焦点,导致整个布局向下滚动。

我想要的是什么:

Edittext不应该失去焦点,每次它应该处于活动状态,等待键盘打开输入。

我尝试过但不起作用:

if (messageCounter % 10 == 0) {

        LinearLayout advertisedMessageLayout = new LinearLayout(this);
        advertisedMessageLayout.setOrientation(LinearLayout.VERTICAL);
        advertisedMessageLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        AdView av = new AdView(this, AdSize.BANNER, MBConstants.ADVIEW_ID);
        //remove focus for every child of adview
        for (int i = 0; i < av.getChildCount(); i++) {
            av.getChildAt(i).setFocusable(false);
            av.getChildAt(i).setFocusableInTouchMode(false);
            av.getChildAt(i).setClickable(false);

        }
        av.setFocusable(false);
        av.setFocusableInTouchMode(false);
        av.setClickable(false);
        av.setEnabled(false);

        AdRequest request = new AdRequest();
        av.loadAd(request);

        advertisedMessageLayout.addView(messageRow);
        advertisedMessageLayout.addView(av);


        return advertisedMessageLayout;

    }

是否有任何可能的方法可以阻止广告素材获得焦点并且表现得像普通视图一样?

感谢。

3 个答案:

答案 0 :(得分:0)

要解决此问题,您需要在onAdLoaded事件中设置AdView布局参数。

@Override
public void onAdLoaded() {
    super.onAdLoaded();
    LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    adView.setLayoutParams(lay);
}

答案 1 :(得分:0)

on onCreate()将adview的可见性设置为GONE

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        .........

        adView.setVisibility(View.GONE)

       ..........

    }

在加载广告之后,将其可见性设置为可见

adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();

            adView.setVisibility(View.VISIBLE)
        }
    });

希望这会有所帮助;)

答案 2 :(得分:0)

Admob WebView在这种情况下获得了关注。要从横幅视图中删除焦点,请从活动中清除焦点或关闭软键盘

    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;
        }
    });

或关闭软键盘

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;
        }
    });