2016-04-26 27 views
0

我試圖在點擊一個按鈕後,在我的應用程序的MainActivity中顯示一個非常簡單的Snackbar。此按鈕還會導致新活動的開始。但點擊它後,沒有小吃店顯示和新的活動開始。我的MainActivity是一個RelativeLayout,我不想把它改成CoordinatorLayout。在小吃吧的簡單的android小吃店不工作

<RelativeLayout 
<TextView 
     android:id="@+id/tvReceived" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Messages Received" 
     android:textSize="25sp" 
     android:gravity="center" 
     android:textStyle="bold" 
     android:textAllCaps="false" 
     android:textColor="#3079ab" 
     android:layout_marginTop="10dp"/> 

    <LinearLayout 
     android:id="@+id/linearMain" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tvReceived" 
     android:layout_marginTop="7dp" 
     android:layout_marginBottom="50dp"> 
     <FrameLayout 
      android:id="@+id/receivedList" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
     </FrameLayout> 
    </LinearLayout> 

    <Button 
     android:id="@+id/newMessage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Message" 
     android:textAllCaps="false" 
     android:textSize="16sp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignEnd="@+id/linearMain" 
     android:layout_marginBottom="32dp" /> 
</RelativeLayout> 

Java代碼:

Button btnNewSms = (Button) findViewById(R.id.newMessage); 
btnNewSms.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Snackbar mySnackbar = Snackbar.make(v, R.string.new_message, Snackbar.LENGTH_LONG); 
     mySnackbar.show(); 
     Intent intent = new Intent(MainActivity.this, ComposeActivity.class); 
     startActivity(intent); 
    } 
}); 

有什麼不對?提前致謝!

+1

評論你的意圖和運行代碼,活動開始時,按鈕被點擊,你無法看到小吃店。試着讓我知道它是否有效 –

+0

沒什麼不對。當第二個啓動時,您無法在第一個「活動」中看到「Snackbar」。如果這就是你的想法,'startActivity()'調用不會等待你的'Snackbar'。 –

+0

啊...是的,它的工作!我可以在Snackbar出現後開始新的活動嗎? CoordinatorLayout是否需要這樣做? – Emile

回答

2

評論您的意圖並運行代碼,Activity開始於單擊該按鈕時,並且您無法看到Snackbar。 您可以通過以下

public void showMsgSnack(String msg) { 

     snackbar = Snackbar.make(getCurrentFocus(), "Your Message here", Snackbar.LENGTH_INDEFINITE).setAction("Open", new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        //Your Intent here 
      } 
     }); 
     snackbar.show(); 
    } 

嘗試打開你的按鈕的點擊ActivitySnackbar裏面,讓我知道它的工作

+0

完美的作品!謝謝! – Emile

+0

@Emile:樂於助人。乾杯! –