1

在我的应用程序中,我试图在 RealViewSwitcher 中切换两个透明按钮的可见性(我知道这是相当骇人听闻的)。我正在根据 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"/> 
4

1 回答 1

1

您的代码只需要稍微清理一下。

  1. 首先,您的按钮应声明为字段。事实上,它们只是 if 语句的实例变量。
  2. 出于同样的原因,应在 if 语句之外声明单击侦听器。onCreate()在您喜欢的地方或任何地方声明它们。
  3. getCurrentWindow(). 它比if... else if... else if....

我可以建议:

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...");
}
于 2012-04-16T23:29:50.467 回答