【概述】java
【Button】android
<Button android:id="@+id/testBtn" android:layout_width="150dp" android:layout_height="43dp" android:onClick="onClick" android:text="測試按鈕" tools:layout_editor_absoluteX="130dp" tools:layout_editor_absoluteY="46dp" />
protected void onClick(View v){ switch (v.getId()){ case R.id.testBtn: Toast.makeText(this, "測試按鈕被點擊", Toast.LENGTH_SHORT).show(); } }
【Toast】web
1). 消息展現的 Context 實例,通常指當前 Activity 實例;app
2). 消息提示的內容:字符串格式,也能夠寫到 R資源中;編輯器
3). 消息提示的時間長度:Toast.LENGTH_SHORT 和 Toast.LENGTH_LONGide
//用戶點擊返回鍵時觸發 private static final int TIME_INTERVAL = 2000; private long mBackPressed = 0; public void onBackPressed(){ long cMills = System.currentTimeMillis(); if(cMills - mBackPressed > TIME_INTERVAL){//若是兩次點擊時間間隔超過 TIME_INTERVAL,則執行「提示退出」操做 mBackPressed = cMills; Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show(); }else{//若是兩次點擊在 TIME_INTERVAL 時間間隔內,則執行「退出」操做 super.onBackPressed(); } }
【Menu】佈局
建立 bottom_nav_menu.xml 完成。測試
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/navigation_home" android:icon="@drawable/ic_home_black_24dp" android:title="@string/title_home" /> <item android:id="@+id/navigation_dashboard" android:icon="@drawable/ic_dashboard_black_24dp" android:title="@string/title_dashboard" /> <item android:id="@+id/navigation_notifications" android:icon="@drawable/ic_notifications_black_24dp" android:title="@string/title_notifications" /> </menu>
<resources>
<string name="app_name">HelloWorld</string>
<string name="title_home">home</string>
<string name="title_dashboard">dashboard</string>
<string name="title_notifications">notifications</string>
</resources>
【menu + fragment 實現菜單切換】this
<LinearLayout android:id="@+id/main_view" android:layout_width="411dp" android:layout_height="546dp" android:layout_weight="1" android:orientation="vertical" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="0dp" />
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".HomeFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Hello Home!" /> </FrameLayout>
package com.xyt.helloworld; import android.os.Bundle; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class HomeFragment extends Fragment { private TextView textView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_home,container,false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); textView=(TextView)getActivity().findViewById(R.id.title); } }
package com.xyt.helloworld; import android.content.Intent; import android.os.Bundle; import com.google.android.material.bottomnavigation.BottomNavigationView; import androidx.appcompat.app.AppCompatActivity; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import android.view.MenuItem; import android.view.View; import android.webkit.WebView; import android.widget.Button; import android.widget.FrameLayout; import android.widget.TextView; import android.widget.Toast; import java.util.*; public class MainActivity extends AppCompatActivity { private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: switchFragment(0); return true; case R.id.navigation_dashboard: switchFragment(1); return true; case R.id.navigation_notifications: switchFragment(2); return true; } return false; } }; private Fragment[] mFragments = new Fragment[5]; private void initMainView(){ mFragments[0] = new HomeFragment(); mFragments[1] = new DashboardFragment(); mFragments[2] = new NotificationFragment(); //初始化第一個 fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.main_view, mFragments[0]); transaction.show(mFragments[0]).commitAllowingStateLoss(); } private int preIdx = 0; private void switchFragment(int curIdx){ if(preIdx != curIdx){//切換內容 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.hide(mFragments[preIdx]);//隱藏上個Fragment if(!mFragments[curIdx].isAdded()){ transaction.add(R.id.main_view, mFragments[curIdx]); } transaction.show(mFragments[curIdx]).commitAllowingStateLoss(); preIdx = curIdx;//更新 index } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView navView = findViewById(R.id.nav_view); navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); initMainView(); } }