在Android Studio 2.3中使用约束布局
尝试实现:在屏幕上显示一个按钮,并隐藏布局中的所有其他元素。在该按钮上单击,显示所有其他元素,但隐藏按钮本身。
已实现:使用布局嵌套,如果我将按钮放在一个布局中,则将其设置为在开始时可见,并将所有元素放在另一个布局中。我可以解决上述问题。
问题:我希望在没有嵌套布局的情况下实现这一点,否则就是使用约束布局。任何人吗?
public class MainActivity extends AppCompatActivity {
Button startButton,playButton;
TextView timerTextView;
TextView resultTextView;
Button button0,button1,button2,button3;
TextView sumTextView;
TextView pointsTextView;
ArrayList<Integer> answers = new ArrayList<Integer>();
int locationOfCorrectAnswer;
int score=0;
int numberOfQuestions=0;
ConstraintLayout mainLayout;
public void playAgain(View view)
{
score = 0;
numberOfQuestions = 0;
timerTextView.setText("30s");
pointsTextView.setText("0/0");
resultTextView.setText("");
playButton.setVisibility(View.INVISIBLE);
generateQuestion();
new CountDownTimer(30100,1000){
@Override
public void onTick(long millisUntilFinished) {
timerTextView.setText(String.valueOf(millisUntilFinished/1000) + "s");
}
@Override
public void onFinish() {
timerTextView.setText("0s");
playButton.setVisibility(View.VISIBLE);
resultTextView.setText("Your score is " + Integer.toString(score)+ " / " + Integer.toString(numberOfQuestions));
}
}.start();
}
public void generateQuestion()
{
Random rand= new Random();
int a = rand.nextInt(21);
int b = rand.nextInt(21);
sumTextView.setText(Integer.toString(a)+ " + " + Integer.toString(b));
locationOfCorrectAnswer = rand.nextInt(4);
answers.clear();
int incorrectAnswer;
for(int i=0; i <4 ; i++)
{
if(i==locationOfCorrectAnswer)
{
answers.add(a + b);
}
else
{
incorrectAnswer = rand.nextInt(41);
while ( incorrectAnswer == a + b)
{
incorrectAnswer = rand.nextInt(41);
}
answers.add(incorrectAnswer);
}
}
button0.setText(Integer.toString(answers.get(0)));
button1.setText(Integer.toString(answers.get(1)));
button2.setText(Integer.toString(answers.get(2)));
button3.setText(Integer.toString(answers.get(3)));
}
public void chooseAnswer(View view)
{
if(view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer)))
{
score++;
resultTextView.setText("Correct!");
}
else
{
resultTextView.setText("Wrong!");
}
numberOfQuestions++;
pointsTextView.setText(Integer.toString(score)+"/"+ Integer.toString(numberOfQuestions));
generateQuestion();
}
public void start(View view)
{
startButton.setVisibility(View.INVISIBLE);
mainLayout.setVisibility(View.VISIBLE);
playAgain(findViewById(R.id.playButton));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.startButton);
sumTextView = (TextView) findViewById(R.id.sumTextView);
button0 = (Button) findViewById(R.id.button2);
button1 = (Button) findViewById(R.id.button3);
button2 = (Button) findViewById(R.id.button4);
button3 = (Button) findViewById(R.id.button5);
resultTextView = (TextView) findViewById(R.id.resultTextView);
pointsTextView = (TextView) findViewById(R.id.pointsTextView);
timerTextView = (TextView) findViewById(R.id.timerTextView);
playButton= (Button) findViewById(R.id.playButton);
mainLayout = (ConstraintLayout) findViewById(R.id.mainLayout);
}
}
答案 0 :(得分:0)
您可以在点击按钮时设置元素的可见性。这方面的一个例子是:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button.setVisibility(View.GONE); //hide button
layout.setVisibility(View.VISIBLE); //show some layout
textview.setVisibility(View.VISIBLE); //show some textview
....
}
});
如果要在布局中保留按钮的空间,请使用View.INVISIBLE
。如果按钮在不可见后不应占用布局中的任何空格,请使用View.GONE
。