MainActivity- package com.example.tony.ch47_collapsingtoolbarlayout;
- import android.support.design.widget.CollapsingToolbarLayout;
- import android.support.design.widget.CoordinatorLayout;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- FloatingActionButton fab;
- CoordinatorLayout cl;
- CollapsingToolbarLayout ctl;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- cl= (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
- ctl= (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
- ctl.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Snackbar.make(cl,"您點擊了上方的Banner",Snackbar.LENGTH_SHORT).show();
- }
- });
- fab= (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Snackbar.make(cl,"查看今日運勢?",Snackbar.LENGTH_INDEFINITE).setAction("確定", new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String str[]={"吉","普通","凶"};
- int r=(int)(Math.random()*3);
- Toast.makeText(MainActivity.this,str[r],Toast.LENGTH_SHORT).show();
- }
- }).show();
- }
- });
- }
- }
複製代碼 |