安卓:复位按钮永久禁用另一个按钮 [英] Android: Reset Button Permanently Disabled Another Button

查看:108
本文介绍了安卓:复位按钮永久禁用另一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有四个按钮。第四个按钮是复位按钮,我加入到活动复位到状态时,它最初开始。然而,当我加入了复位按钮,它的按钮永久性残疾之一,尽管previously,正在启用/禁用按钮他们应该的方式。下面是相关code:

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_name);

    //非相关code

    okButton =(按钮)findViewById(R.id.ok);
    okButton.setOnClickListener(本);

    changeButton =(按钮)findViewById(R.id.change_button);
    changeButton.setOnClickListener(本);

    Next按钮=(按钮)findViewById(R.id.next_button);
    nextButton.setEnabled(假);
    nextButton.setOnClickListener(本);

    复位=(按钮)findViewById(R.id.reset);
    reset.setEnabled(假);
    reset.setOnClickListener(本);
}

@覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
    案例R.id.ok:
        changeButton.setEnabled(假);
        okButton.setEnabled(假);
        nextButton.setEnabled(真正的);
        打破;

    案例R.id.change_button:
        changeButton.setEnabled(假);
        okButton.setEnabled(假);
        nextButton.setEnabled(真正的);
        打破;

    案例R.id.next_button:
        nextButton.setEnabled(假);
        okButton.setEnabled(真正的);
        changeButton.setEnabled(真正的);
        打破;

    案例R.id.reset:
        意向意图= getIntent();
        完();
        startActivity(意向);
        打破;

    默认:
        打破;
    }
}
 

一切工作,因为它应该有,直到所有涉及到的复位按钮的部分增加了。从本质上讲,我想要的行为是:

  • 起初:下一个和复位是残疾人,他们成为被允许,在改变或确定被点击

  • 在更改或确定被点击,他们都被禁用(以prevent点击一次以上)和两个复位和明年启用

  • 在一个或复位被点击,他们成为残疾人和变化​​,确定被启用。

的变化,确定,下一步按钮行为进行工作,直到复位code加入。那么接下来按钮成为永久停用。哪里不对?如何解决呢?

编辑*:

下面是XML $ C $下按钮:

 <按钮
    机器人:ID =@ + ID / OK
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBaseline =@ + ID / change_button
    机器人:layout_alignBottom =@ + ID / change_button
    机器人:layout_toLeftOf =@ + ID /复位
    机器人:文本=@字符串/ OK/>

<按钮
    机器人:ID =@ + ID / next_button
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBaseline =@ + ID / change_button
    机器人:layout_alignBottom =@ + ID / change_button
    机器人:layout_alignParentLeft =真
    机器人:文本=@字符串/ next_button/>

<按钮
    机器人:ID =@ + ID /复位
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentRight =真
    机器人:文本=@字符串/复位/>

<按钮
    机器人:ID =@ + ID / change_button
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_marginBottom =14dp
    机器人:layout_toRightOf =@ + ID / next_button
    机器人:文本=@字符串/更改/>
 

解决方案
  

起初:下一个和复位是残疾人,他们被启用时,   更改或确认被点击

  @覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
    案例R.id.ok:
        changeButton.setEnabled(假);
        okButton.setEnabled(假);
        nextButton.setEnabled(真正的);
        reset.setEnabled(真正的); //添加此
        打破;

    案例R.id.change_button:
        changeButton.setEnabled(假);
        okButton.setEnabled(假);
        nextButton.setEnabled(真正的);
        reset.setEnabled(真正的); //添加此
        打破;

    案例R.id.next_button:
        nextButton.setEnabled(假);
        okButton.setEnabled(真正的);
        changeButton.setEnabled(真正的);
       reset.setEnabled(假); //添加此
        打破;

    案例R.id.reset:
        意向意图= getIntent();
        完();
        startActivity(意向);
        打破;

    默认:
        打破;
    }
}
 

这为我工作/添加的行。

I currently have four buttons. The fourth button is a reset button I added to reset the activity to the state when it was originally started. However, when I added the reset button it disabled one of the buttons permanently, even though previously, the buttons were being enabled/disabled the way they were supposed to. Here is the relevant code:

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

    // non relevant code

    okButton = (Button) findViewById(R.id.ok);
    okButton.setOnClickListener(this);

    changeButton = (Button) findViewById(R.id.change_button);
    changeButton.setOnClickListener(this);

    nextButton = (Button) findViewById(R.id.next_button);
    nextButton.setEnabled(false);
    nextButton.setOnClickListener(this);

    reset = (Button) findViewById(R.id.reset);
    reset.setEnabled(false);
    reset.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.ok:
        changeButton.setEnabled(false);
        okButton.setEnabled(false);
        nextButton.setEnabled(true);
        break;

    case R.id.change_button:
        changeButton.setEnabled(false);
        okButton.setEnabled(false);
        nextButton.setEnabled(true);
        break;

    case R.id.next_button:
        nextButton.setEnabled(false);
        okButton.setEnabled(true);
        changeButton.setEnabled(true);
        break;

    case R.id.reset:
        Intent intent = getIntent();
        finish();
        startActivity(intent);
        break;

    default:
        break;
    }
}

Everything was working as it should have until all of the parts involving the reset button were added. Essentially, the behaviour I want is:

  • Initially: next and reset are disabled, they become enabled when either change or ok are clicked

  • When either change or ok are clicked, they both are disabled (to prevent clicking more than once) and both reset and next are enabled

  • When next or reset are clicked, they become disabled and change and ok become enabled.

The change, ok, and next button behaviors were working until the reset code was added. Then the next button became permanently disabled. What is wrong? How do I fix it?

EDIT*:

Here is the xml code for the buttons:

<Button
    android:id="@+id/ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/change_button"
    android:layout_alignBottom="@+id/change_button"
    android:layout_toLeftOf="@+id/reset"
    android:text="@string/Ok" />

<Button
    android:id="@+id/next_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/change_button"
    android:layout_alignBottom="@+id/change_button"
    android:layout_alignParentLeft="true"
    android:text="@string/next_button" />

<Button
    android:id="@+id/reset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="@string/reset" />

<Button
    android:id="@+id/change_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="14dp"
    android:layout_toRightOf="@+id/next_button"
    android:text="@string/change" />

解决方案

Initially: next and reset are disabled, they become enabled when either change or ok are clicked

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.ok:
        changeButton.setEnabled(false);
        okButton.setEnabled(false);
        nextButton.setEnabled(true);
        reset.setEnabled(true); // add this
        break;

    case R.id.change_button:
        changeButton.setEnabled(false);
        okButton.setEnabled(false);
        nextButton.setEnabled(true);
        reset.setEnabled(true); // add this
        break;

    case R.id.next_button:
        nextButton.setEnabled(false);
        okButton.setEnabled(true);
        changeButton.setEnabled(true);
       reset.setEnabled(false); // add this
        break;

    case R.id.reset:
        Intent intent = getIntent();
        finish();
        startActivity(intent);
        break;

    default:
        break;
    }
}

This worked for me/ Add those lines.

这篇关于安卓:复位按钮永久禁用另一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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