Android:将按钮设置为不可见

时间:2015-06-03 07:44:20

标签: android button dynamic visibility

双击时,按钮可以切换为可见/不可见。我动态创建了我的布局。它的工作原理如果btns最初是可见的,但我希望它们最初是不可见的。

我的DoubleTap fn是

 <?php 
// Parse the log in form if the user has filled it out and pressed "Log In"
if (isset($_POST["user_name"]) ) {

        $user = mysql_real_escape_string($_POST["user_name"]);      
        $pass_word = mysql_real_escape_string(md5($_POST["pass_word"]));
   $pass_word=md5($pass_word);

    // Connect to the MySQL database  
    include "../connect_to_mysql.php"; 
    $sql = mysql_query("SELECT m_id FROM member WHERE user_name='$user' AND pass_word='$pass_word' LIMIT 1"); // query the person
     // ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
    $existCount = mysql_num_rows($sql); // count the row nums
    if ($existCount == 1) { // evaluate the count
         while($row = mysql_fetch_array($sql)){ 
             $id = $row["m_id"];
     }
         $_SESSION["m_id"] = $id;
         $_SESSION["user"] = $user;
         $_SESSION["pass_word"] = $pass_word;

         header("location: ../../index.php");
         exit();
    } else {
        echo 'That information is incorrect, try again <a     href="member_login.php">Click Here</a>'; 
        exit();
    }
}
?>

其中&#34;可见&#34;是一个布尔值。

我在onCreate()和onStart()

中尝试过这些
public boolean onDoubleTap(MotionEvent arg0) {
    // TODO Auto-generated method stub

    if (visible)
    {
        b1.setVisibility(View.INVISIBLE);
        b2.setVisibility(View.INVISIBLE);
        b3.setVisibility(View.INVISIBLE);
        b4.setVisibility(View.INVISIBLE);
        visible = false;
    }
    else
    {
        b1.setVisibility(View.VISIBLE);
        b2.setVisibility(View.VISIBLE);
        b3.setVisibility(View.VISIBLE);
        b4.setVisibility(View.VISIBLE);
        visible = true;
    }
    return false;
}

但如果我添加它,它在双击时不会显示。我通过Toast检查了它们,它被执行但没有显示出来。

任何人都可以提供帮助。修改或新代码都可以正常工作。

提前致谢...

3 个答案:

答案 0 :(得分:0)

 Button btn=new Button(getBaseContext());
 btn.setVisibility(View.INVISIBLE);

在创建按钮时尝试此操作

答案 1 :(得分:0)

如果您正在参加活动,那么

Button btn = new Button(YourActivity.this);
btn.setVisibility(View.INVISIBLE);

如果你是片段,那么:

Button btn = new Button(getActivity());
btn.setVisibility(View.INVISIBLE);

如果你想在onCreate方法中使按钮不可见。假设你已经在onCreate中完成了这个:

Button btn = new Button(this);
btn.setVisibility(View.INVISIBLE);

然后你必须反复做你在触摸时所做的事情。

答案 2 :(得分:0)

我怀疑问题是git时按钮无法点击。正确的做法是让他们View.INVISIBLE

Have a look at this question and the related answer!

修改:This question might help too!