【问题标题】:Value of buttons to be changed with click of next and prev [duplicate]通过单击下一个和上一个来更改按钮的值[重复]
【发布时间】:2013-01-24 10:15:26
【问题描述】:

可能重复:
Button next and prev not working

我的布局中有 5 个按钮,在下一个按钮上我想更改来自 arraylist 的按钮的文本。我该如何实现呢?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center_horizontal"
android:orientation="vertical" >

    <LinearLayout style="@style/verticallayout" >

        <Button
            android:id="@+id/b1"
            style="@style/buttons"
            android:layout_weight=".16"
            android:text="1" />
    </LinearLayout>

    <LinearLayout style="@style/verticallayout" >

        <Button
            android:id="@+id/b2"
            style="@style/buttons"
            android:layout_weight=".16"
            android:text="2 />
    </LinearLayout>

    <LinearLayout style="@style/verticallayout" >

        <Button
            android:id="@+id/b3"
            style="@style/buttons"
            android:layout_weight=".16"
            android:text="3" />
    </LinearLayout>

    <LinearLayout style="@style/verticallayout" >

        <Button
            android:id="@+id/b4"
            style="@style/buttons"
            android:layout_weight=".16"
            android:text="4" />
    </LinearLayout>

    <LinearLayout style="@style/verticallayout" >

        <Button
            android:id="@+id/b5"
            style="@style/buttons"
            android:layout_weight=".16"
            android:text="5" />
    </LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.5"
        android:enabled="false"
        android:text="Prev" />

    <Button
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.5"
        android:text="Next" />
</LinearLayout>

我的 java 类:: 这里的 count 是静态变量。

public void onClick(View arg0) {
    String a=null;
    switch(arg0.getId()){
    case R.id.left:


        if( count==0)
            prev.setEnabled(false);
        else{
             count--;

        }
        break;
    case R.id.right:
        if( count>5)
            next.setEnabled(false);
        else{
             count++;

        }
        break;
    case R.id.b1:

            a=(( count*0)+0)+"";
            bb1.setText(a);
        }
        break;
    case R.id.b2:

            a= (( count*0)+1)+"";
            bb2.setText(a);
        }
        break;
    case R.id.b3:

            a= (( count*0)+2)+"";
            bb3.setText(a);
        }
        break;
    case R.id.b4:

            a= ((count*0)+3)+"";
            bb4.setText(a);
        }
        break;
    case R.id.b5:

            a= ((count*0)+4)+"";
            bb5.setText(a);
        }
        break;

    }
}

我希望通过计数值更改按钮文本。但价值观没有改变。 我的疑问是每次点击下一个和上一个后是否需要刷新活动

【问题讨论】:

  • 我想根据计数更改 b1,2,3,4,5 的文本值。如果单击下一个,则值分别为 shd b 5,6,7,8并且在 prev 它 shd b 递减。

标签: java android android-layout button android-intent


【解决方案1】:

问题是,您正在尝试更改按钮自身单击事件中的文本。

因此,当您单击下一个或上一个时,按钮中的文本不会更改(仅计数更改)。当您单击它们时,它们只会更改(实际上更新为最新的计数值)(b1,2,3,4,5)。

所以,为了得到你需要的东西,这样做:

public void onClick(View arg0) {
String a=null;
switch(arg0.getId()){
case R.id.left:


    if( count==0)
        prev.setEnabled(false);
    else{
         count--;
         //You want to change the text(count) of button1,2,3,4,5 when you click prev.
         //So, do that here

bb1.setText("" + count);
bb2.setText("" + count);
bb3.setText("" + count);
bb4.setText("" + count);
bb5.setText("" + count);

         //Similarly on next clicked

    break;
case R.id.right:
    if( count>5)
        next.setEnabled(false);
    else{
         count++;
bb1.setText("" + count);
bb2.setText("" + count);
bb3.setText("" + count);
bb4.setText("" + count);
bb5.setText("" + count);

    break;
case R.id.b1:
        //here don't change any text, just check the text value and call the respective answer
       if(bb1.getText == "5"){
          //call answer5 etc..,
          }

          //do the same with other buttons b2,3,4,5
    break;
case R.id.b2:

        a= (( count*0)+1)+"";
        bb2.setText(a);

    break;
case R.id.b3:

        a= (( count*0)+2)+"";
        bb3.setText(a);

    break;
case R.id.b4:

        a= ((count*0)+3)+"";
        bb4.setText(a);

    break;
case R.id.b5:

        a= ((count*0)+4)+"";
        bb5.setText(a);

    break;

}

}

通过它您在单击下一个和上一个按钮时更改按钮的文本

【讨论】:

  • 那么我如何提供这些按钮的 onclick 功能?
  • 让我澄清一件事。您想在单击下一个或上一个按钮时更改 b1,2,3,4,5 的文本吗?然后,您必须在下一个和上一个按钮的 onClick 中实现该文本更改,而不是在 b1、2、3、4、5 的 onClick 中实现。之后,如果您想为 b1,2,3,4,5 提供 onClick 功能,请在 b1,2,3,4,5 的 onClick 中添加这些功能,这是您的 switch 块中的最后 5 个案例
  • 从您的 b1,2,3,4,5 代码 onClick 中,您正试图更改文本,但没有别的。哪个应该在下一个和上一个按钮的 onClick 中
  • 我还有一些在 b12345 中工作的功能。根据文本,按钮的功能会发生变化。如果文本为 8,则在单击按钮单击时显示第 8 个答案。所以我想要文本和 onclick 在同一个地方。
  • 你说你的按钮文本没有改变。所以,现在点击下一步——> b1,2,3,4,5 的文本不会改变。现在单击 b1,2,3,4,5 中的任何一个--> 这次单击按钮的文本发生了变化。那是因为您在 b1,2,3,4,5 中实现了文本更改。让我添加完整的代码和解释。
【解决方案2】:

您是否缺少此代码? :

Button Button1= (button)findviewbyid(R.id.right);
Button1.setonclicklistener(this);

【讨论】:

  • 我在 onclick 函数上面有它。我只是添加了 onclik 功能
【解决方案3】:

在您的 onClick() 方法中,逻辑不正确..只需要两种情况..

当您按下下一个或上一个按钮时,您可以更改剩余五个按钮的文本

解决了....

【讨论】:

  • 我也为他们准备了按钮功能。否则我会使用文本视图,如果它只是改变文本的问题..
猜你喜欢
  • 2020-09-21
  • 1970-01-01
  • 2014-02-25
  • 2017-01-16
  • 2016-07-26
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 2014-04-19
相关资源
最近更新 更多