如何隐藏微调下拉android [英] How to hide spinner dropdown android

查看:28
本文介绍了如何隐藏微调下拉android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在外部点击时隐藏微调提示弹出窗口.如果提示弹出窗口打开并且用户按下主页键活动将最小化,因此当用户再次打开应用程序时,提示弹出窗口应该消失.
有什么办法可以做到这一点.谢谢

I want to hide spinner prompt popup on outside click. If prompt popup is open and user press home key activity will minimize so when user again open application that prompt popup should disappear.
Is there any way to achieve this. Thank You

-- 未自定义提示弹出窗口.所以我不能在 onPauseonResume 方法中隐藏它们.

-- Prompt popup is not customized. So I can't hide them in onPause or onResume methods.

推荐答案

嗯,它比我想象的要复杂一些.

Well its a little complicated than I thought.

我在这里添加了分步详细信息.尝试遵循它.我能够在 api 级别 10 中实现这一点.

I am adding the step by step details here. Try to follow it. I was able to achieve this in api level 10.

并且此解决方案假定您应该在用户单击主页按钮时以编程方式关闭提示对话框,或者如果您必须在没有用户交互的情况下移动到下一个活动

第一步是通过扩展 Spinner 类来创建自定义 Spinner.假设,我在 com.bts.sampleapp

The first step is to create a Custom Spinner by extending Spinner Class. Let's say, I have created a class called CustomSpinner in the package com.bts.sampleapp

我的 CustomSpinner 类看起来像这样,

My CustomSpinner class looks like this,

package com.bts.sampleapp;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;

    public class CustomSpinner extends Spinner{
        Context context=null;

        public CustomSpinner(Context context) {
            super(context);
            this.context=context;
        }

        public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public CustomSpinner(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        public void onDetachedFromWindow() {
            super.onDetachedFromWindow();
        }
    }

现在在你的 Xml 文件中,用这个自定义微调器替换微调器元素,

Now in your Xml file, replace Spinner element by this custom spinner,

        <com.bts.sampleapp.CustomSpinner
        android:id="@+id/spin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

下一步是在你的Activity类中初始化和设置适配器到这个微调器,

The next step is to initialize and set adapter to this spinner in your Activity class,

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CustomSpinner spin=null;
    spin=(CustomSpinner)findViewById(R.id.spin);
    spin.setAdapter(spinnerAdapter); //you can set your adapter here.
}

最后一步是在用户单击 HomeButton 或 Activity 移至后台时关闭对话框.为此,我们像这样覆盖 onPause(),

The final step is to close the dialog when the user clicks on HomeButton or When the Activity moves to background. To do this, we override the onPause() like this,

@Override
    protected void onPause() {
        Log.i("Life Cycle", "onPause");
        spin.onDetachedFromWindow();
        super.onPause();
    }

现在在 onPause() 中调用方法 spin.onDetachedFromWindow(); 为您完成关闭提示对话框的工作.

Now within the onPause() call the method spin.onDetachedFromWindow(); which does the job of closing the prompt dialog for you.

同时从 Acitivity 的任何位置调用 spin.onDetachedFromWindow(); 应该关闭 Spinner 提示对话框(如果它打开).

Also calling spin.onDetachedFromWindow(); from anywhere within the Acitivity should close the Spinner prompt dialog if it is open.

这篇关于如何隐藏微调下拉android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆