android studio 中从布局文件到 MainActivity.java 的控制流
Control flow in android studio from layout file to MainActivity.java
下面是一个 android 应用程序的代码片段,该应用程序生成一个 1-20 的随机数,用户猜测该数字,当猜完数字后按下按钮时,用户输入的数字 (val ) 和应用程序生成的 (rand_no) 进行比较。
比较后我希望显示的文本消失,以便每次进行猜测并按下按钮时生成新的输出。
我每次调用函数时(按下按钮时)都将可见性设置为 INVISIBLE,然后在进行比较并显示输出后再次将可见性设置为 VISIBLE。
但令我惊讶的是,该操作仅发生一次,并且在第一次函数调用后文本不再可见。
public class MainActivity extends AppCompatActivity {
Random random=new Random();
int rand_no=random.nextInt(20)+1;
public void function(View v)
{
EditText e1=(EditText)findViewById(R.id.editText); //for text input by
//the user
TextView e2=(TextView) findViewById(R.id.textOutput); //for output text
int val=Integer.parseInt(e1.getText().toString());
e2.setVisibility(View.INVISIBLE); //setting output to INVISIBLE
if(rand_no<val)
{
e2.setText("Go Lower!");
}
if(rand_no>val)
{
e2.setText("Go Higher!");
}
if(rand_no==val)
{
e2.setText("You guessed right!");
}
e2.setVisibility(View.VISIBLE); //setting output to VISIBLE
/* Fading away the output*/
e2.animate().setStartDelay(2000);
e2.animate().alpha(0).setDuration(1000);
}
因此我想知道函数结束后,控制又传给了
布局文件?或者它保留在 MainActivity.java 中,因为即使我们多次按下按钮以使该功能再次执行,可见性似乎只分配一次。
e2.animate().alpha(0).setDuration(1000);
你正在淡化它,所以你需要让它再次可见。
用下面的代码替换引用行
e2.animate().alpha(0).setDuration(1000)
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
e2.animate().alpha(1).setDuration(500);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
下面是一个 android 应用程序的代码片段,该应用程序生成一个 1-20 的随机数,用户猜测该数字,当猜完数字后按下按钮时,用户输入的数字 (val ) 和应用程序生成的 (rand_no) 进行比较。
比较后我希望显示的文本消失,以便每次进行猜测并按下按钮时生成新的输出。
我每次调用函数时(按下按钮时)都将可见性设置为 INVISIBLE,然后在进行比较并显示输出后再次将可见性设置为 VISIBLE。 但令我惊讶的是,该操作仅发生一次,并且在第一次函数调用后文本不再可见。
public class MainActivity extends AppCompatActivity {
Random random=new Random();
int rand_no=random.nextInt(20)+1;
public void function(View v)
{
EditText e1=(EditText)findViewById(R.id.editText); //for text input by
//the user
TextView e2=(TextView) findViewById(R.id.textOutput); //for output text
int val=Integer.parseInt(e1.getText().toString());
e2.setVisibility(View.INVISIBLE); //setting output to INVISIBLE
if(rand_no<val)
{
e2.setText("Go Lower!");
}
if(rand_no>val)
{
e2.setText("Go Higher!");
}
if(rand_no==val)
{
e2.setText("You guessed right!");
}
e2.setVisibility(View.VISIBLE); //setting output to VISIBLE
/* Fading away the output*/
e2.animate().setStartDelay(2000);
e2.animate().alpha(0).setDuration(1000);
}
因此我想知道函数结束后,控制又传给了 布局文件?或者它保留在 MainActivity.java 中,因为即使我们多次按下按钮以使该功能再次执行,可见性似乎只分配一次。
e2.animate().alpha(0).setDuration(1000);
你正在淡化它,所以你需要让它再次可见。
用下面的代码替换引用行
e2.animate().alpha(0).setDuration(1000)
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
e2.animate().alpha(1).setDuration(500);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});