简体   繁体   English

在Android中单击按钮后如何锁定微调器

[英]How to lock the spinner after button click in android

How can I prevent the user to change the value selected in spinner after some event like button press? 在按下按钮之类的事件后,如何防止用户更改spinner选择的值?

It is like after selecting the desired value from spinner the user will press a button after which they will not be allowed to see the values in the spinner except the top value ie the selected one so that no change be made in the selected value. 就像在从spinner选择所需的值之后,用户将按下一个button此后将不允许他们查看spinner除最高值(即选定的值)之外的值,从而不会更改选定的值。

UPDATE:- And yes the spinners are also created dynamically on the press of that same button so it should lock only the previously created spinners not the new one. 更新: -是的spinners也被动态创建上的按下same button ,所以应该只锁定先前创建的微调不是新的。

Code for dynamic creation of Layout which contains the Spinner along with an EditText & a Button :- 动态创建Layout代码,其中包含Spinner以及EditTextButton

 View.OnClickListener addListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);

            newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
            btnRemove.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    container.removeView(newView);
                }
            });

            container.addView(newView);

简单只需在按钮单击上禁用微调器

spinner.setEnabled(false);

In your onClick method, you can just set the visibility to GONE to remove the view as shown below: 在onClick方法中,您只需将可见性设置为GONE即可删除视图,如下所示:

View.OnClickListener addListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);

        newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
        btnRemove.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                newView.setVisibility(View.GONE);  // remove (hide) your view
            }
        });

        container.addView(newView);

And to enable it again, you can set its visibility to VISIBLE as shown below: 并再次启用它,可以将其可见性设置为VISIBLE,如下所示:

newView.setVIsibility(View.VISIBLE);

Or you could just show/hide your spinner as shown below: 或者您可以只显示/隐藏微调器,如下所示:

your_spinner.setVisibility(View.GONE);  // to remove (hide)
your_spinner.setVisibility(View.VISIBLE);  // to make it visible

If you want to only disable the spinner: 如果只想禁用微调器:

your_spinner.setEnabled(false);

Hope it helps. 希望能帮助到你。

You need to create a variable (may be boolean flag = true ) 您需要创建一个变量(可能是boolean flag = true

On button click change to flag = false 在按钮上单击更改为flag = false

And inside Listener to Spinner check if flag is true of false 然后在Spinner的Listener内检查flag是否为true或false

Example

//inside listener of spinner //微调器的内部监听器

if(flag){
//do task
}else{
//restrict the task or don't do anything or display message
}

For Updated question 对于更新的问题

Then you should use disable() method. 然后,您应该使用disable()方法。 spinner1.disable() or spinner2.disable() or so on.. spinner1.disable()或spinner2.disable()等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spinner Android单击中显示和隐藏按钮 - how i can show and hide button in click on spinner android 如何填充在Android中单击按钮时动态生成的微调框 - How to populate a spinner which is dynamically generated on click of button in android 在单击侦听器上设置了按钮的Android Spinner项目 - Android Spinner Item With Button Set On Click Listener Android:单击按钮时获取 Spinner 的 Textview 值 - Android: Get Textview value of Spinner on Button click 第一次单击android后如何禁用按钮 - How to Disable Button after first Click in android Android单击按钮即可删除动态创建的微调器和按钮 - Android remove dynamically created spinner and button with click of a button 当项目在strings.xml中列出而不是在Android Studio中作为静态时,如何删除在Spinner中选择的项目按钮? - How to delete the item selected in a Spinner on button click when the items are listed in strings.xml and not as static in Android Studio? 如何检测微调框是否在Android上单击打开 - How to detect if the Spinner is click open on Android 如何在Android中单击按钮后从GridView中删除按钮? - How to delete button from GridView after click the button in Android? 单击微调器元素android - Click on spinner element android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM