2012-04-16 141 views
1

在我的应用程序im试图切换两个透明按钮(我知道这是相当hackish)在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 = { "[email protected]" }; 
       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"/> 
+2

不要将呼叫之类的硬编码setVisibility int类型。总是使用View.GONE等。 – 2012-04-16 23:12:05

+0

if else if(realViewSwitcher.getCurrentScreen()== 2)else else(realViewSwitcher.getCurrentScreen()== 1)(change 2 to 1)?如果你可以发布你的完整的布局XML,这将是有益的。 – 2012-04-16 23:14:50

+0

它必须等于2,因此指的是realViewSwitcher的第三页。只有第一页和第三页有超链接活动。第二和第四页只是文字。我的xml会立刻起来,谢谢。 – Jamesar 2012-04-16 23:20:25

回答

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 = { "[email protected]" }; 
     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..."); 
} 
+0

这真棒!感谢您的帮助,我会给它一个机会! – Jamesar 2012-04-16 23:34:45