此代码作为启动。请帮帮我:(
public class MainActivity extends Activity {
int i=0;
TextView text;
Button YES,NO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
text = (TextView) findViewById(R.id.text);
text.setText("hello "+i);
YES = (Button) findViewById(R.id.YES);
YES.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
i=i+1;
text.setText("hello "+i);
}
});
NO = (Button) findViewById(R.id.NO);
NO.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
i=i-1;
text.setText("hello "+i);
}
});
}
}
答案 0 :(得分:0)
它仍会更改text
上的TextView
,如:(你好1,你好2,......)
尝试使用不同的text
以便更好地理解:
final Button YES = (Button) findViewById(R.id.YES);
YES.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
i=i+1;
text.setText("Yes button clicked.... hello: " + i);
}
});
final Button NO = (Button) findViewById(R.id.NO);
NO.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
i=i-1;
text.setText("No button clicked.... hello: " + i);
}
});
<强>更新强>
我想改变&#34;你好1&#34;到了#34;你还好吗?&#34;然后,如果他/她点击 YES按钮,textview将显示不同的问题,例如&#34; Are 你快乐吗?&#34;。如果他/她点击否按钮,文本视图将会 显示&#34;你难过吗?&#34;。你知不知道怎么?在回答了几个之后 问题将显示最终结果。你能救我吗?
试试这个:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class Main6Activity extends AppCompatActivity {
TextView text;
Button buttonYES;
Button buttonNO;
ArrayList<String> questionList;
int currentQuestion = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
text = (TextView) findViewById(R.id.text);
buttonYES = (Button) findViewById(R.id.YES);
buttonNO = (Button) findViewById(R.id.NO);
// Dummy questions
questionList = new ArrayList<String>();
questionList.add("Are you okey?");
questionList.add("Are you happy?");
questionList.add("Are you sad?");
questionList.add("Are you fine?");
questionList.add("Are you angry?");
showQuestion();
buttonYES.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
currentQuestion++;
showQuestion();
}
});
buttonNO.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
currentQuestion++;
showQuestion();
}
});
}
public void showQuestion() {
if (currentQuestion < questionList.size() - 1)
{
text.setText(questionList.get(currentQuestion));
}
else
{
text.setText("FINISHED");
}
}
}
希望这有助于理解。