2016-03-08 80 views
-2

我正在开发一个问题游戏,我想在用户点击按钮时更改文本区域中的问题(是/否),以便用户有下一个问题和游戏进行。单击按钮时如何更改文本区域中的文本?

xml code for the button : 


<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Yes" 
    android:id="@+id/btnuserRYes" 
    android:textStyle="bold" 
    android:textSize="16dp" 
    android:layout_marginRight="20dp" 
    android:typeface="serif" 
    android:background="@drawable/yesbutton" 
    android:onClick="Yesbtnclicked" 
/> 

Xml Code for another button: 
<Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/category2" 
      android:id="@+id/btnanimal" 
      android:layout_alignTop="@+id/btncricket" 
      android:layout_toRightOf="@+id/lblofflinemode" 
      android:background="@drawable/mybutton" 
      android:height="50dp" 
      android:onClick="onClickanimal" 
      android:layout_gravity="center_vertical" 
      android:textSize="16dp" 
      android:textStyle="italic" 
      android:layout_marginLeft="20dp" 
      android:width="120dp" 
      android:typeface="serif" /> 

回答

0

您必须添加OnClickListener()您YES/NO按钮

btnuserRYes.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      //change the text of any text area 
    } 
    }); 
+0

已经做到了人..但仍然是我的应用程序崩溃时,我按下按钮 – user6034149

+0

能否请您粘贴相关代码和异常日志? –

0

我建议考虑建筑和你的问题的结构,答案让优质应用。

如果说,关于你的问题:

userbutton = (Button) findViewById(R.id.btnuserRYes); 
userbutton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       //save answer to database 
       //... 
       //change text of UI elements 
       //you can load info from database 
       userbutton.setText("Your next text"); 
      } 
     }); 
相关问题