Java 单击“下一步”和“上一步”可更改的按钮值
可能重复:Java 单击“下一步”和“上一步”可更改的按钮值,java,android,android-layout,button,android-intent,Java,Android,Android Layout,Button,Android Intent,可能重复: 我的布局中有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"
我的布局中有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>
我想用count的值来更改按钮文本。但价值观没有改变。
我的疑问是,是否需要在每次单击“下一步”和“上一步”后刷新AccActivity?是否缺少此代码
Button Button1= (button)findviewbyid(R.id.right);
Button1.setonclicklistener(this);
在onClick()方法中,逻辑不正确。只需要两种情况
按下“下一步”或“上一步”按钮时,可以更改其余五个按钮的文本
这就解决了……问题是,您试图在按钮自身的单击事件中更改按钮的文本 因此,当您单击“下一步”或“上一步”时,按钮中的文本不会更改(仅计数更改)。当您单击它们(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;
}
}
单击“下一步”和“上一步”按钮更改按钮文本的方法我想根据计数更改b1,2,3,4,5的texxt值。。如果单击next,则值shd b分别为5,6,7,8,上一个值shd b递减。我在onclick函数上方有它。。我刚刚添加了onclik功能我还为他们提供了按钮功能。否则我会选择文本视图,如果它不是改变文本的问题。那么我如何提供这些按钮的onclick功能呢?让我澄清一件事。单击“下一步”或“上一步”按钮时,是否要更改b1、2、3、4、5的文本?然后,您必须在下一个和上一个按钮的onClick中实现该文本更改,但不能在b1、2、3、4、5的onClick中实现。之后,如果您想将onClick功能赋予b1,2,3,4,5,请将这些功能添加到onClick的b1,2,3,4,5中,这是切换块中的最后5种情况,从代码onClick的b1,2,3,4,5中可以看出,您正在尝试更改文本,但没有其他更改。它应该在点击下一个和上一个按钮时出现。我在b12345中没有更多的功能。根据文本,功能在按钮外更改。如果文本为8,则单击按钮时会显示第8个答案。所以我希望文本和onclick在同一个位置。你说你的按钮文本没有改变。因此,现在单击下一步-->b1,2,3,4,5的文本不会更改。现在单击b1,2,3,4,5-->中的任意一个-->这一次单击按钮的文本将发生更改。这是因为您在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;
}