切换多个按钮可见性

时间:2012-04-16 23:10:11

标签: java android android-layout android-intent visibility

在我的应用程序中,我试图在RealViewSwitcher中切换两个透明按钮的可见性(我知道这是相当hackish)。我正在根据RealViewSwitcher的当前页面更改可见性。我可以得到第一个按钮,但第二个按钮永远不会变为活动状态。这是我的代码:

///////////////

    if(realViewSwitcher.getCurrentScreen() == 0)
    {

        final Button btn1 = (Button)findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener()
        {       
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setData(Uri.parse("http://www.test.com"));
                startActivity(intent);

                btn1.setVisibility(View.GONE);

            }
        });
    } 

    else if(realViewSwitcher.getCurrentScreen() == 2)
    {
        final Button btn2 = (Button)findViewById(R.id.btn2);
        btn2.setVisibility(0);

        btn2.setOnClickListener(new View.OnClickListener()
        {       
            @Override
            public void onClick(View v) 
            {

                Intent intent = new Intent(Intent.ACTION_SEND); 
                String[] tos = { "info@email.com" }; 
                intent.putExtra(Intent.EXTRA_EMAIL, tos); 
                intent.putExtra(Intent.EXTRA_TEXT, "body"); 
                intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
                intent.setType("message/rfc882"); 
                Intent.createChooser(intent, "Choose Email Client");

            }
        });
    }

    ///////////////
    //end
    /////////////////////

这是xml

    <Button 
        android:id="@+id/btn1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@null"/>

    <Button 
        android:id="@+id/btn2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@null"
        android:visibility="gone"/> 

1 个答案:

答案 0 :(得分:1)

您的代码只需要一点清洁。

  1. 首先,您的按钮应声明为字段。实际上,它们只是if语句的实例变量。
  2. 出于同样的原因,应在if语句之外声明click侦听器。在onCreate()中或在您喜欢的任何地方声明它们。
  3. getCurrentWindow()设置开关。使用它比if... else if... else if...更容易。
  4. 我可以建议:

    final Button btn1 = (Button) findViewById(R.id.btn1);
    final Button btn2 = (Button )findViewById(R.id.btn2);
    
    //Inside onCreate() or similar
    btn1.setOnClickListener(new View.OnClickListener() {       
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("http://www.test.com"));
    
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  //Required to start a new activity
            startActivity(intent);
    
            btn1.setVisibility(View.GONE);
    
        }
    });
    
    //In the same place
    btn2.setOnClickListener(new View.OnClickListener() {       
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SEND); 
            String[] tos = { "info@email.com" }; 
            intent.putExtra(Intent.EXTRA_EMAIL, tos); 
            intent.putExtra(Intent.EXTRA_TEXT, "body"); 
            intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
            intent.setType("message/rfc882"); 
            Intent.createChooser(intent, "Choose Email");
            btn2.setVisibility(View.VISIBLE);
        }
    });
    
    //Later, in your other functional code
    switch (realViewSwitcher.getCurrentScreen()) {
        case 0:
            //do your stuff
            break;
        case 2:
            //other stuff
            break;
        default: //If you need it
            throw new Exception("Oops...");
    }