簡體   English   中英

Android:帶有藍牙和圖形繪制的選項卡式活動,如何?

[英]Android: Tabbed activity with bluetooth and graph plotting, how to?

我是Android的新手,但我想為以后的項目開發自己的應用程序。 這樣做的基本思想是有2個選項卡(目前)。

第一個選項卡,藍牙,將具有一個按鈕,用於在Android和藍牙模塊之間建立連接。

第二個選項卡“圖形”需要讀取輸入流並獲取此數據以繪制隨時間變化的圖形。

現在我的問題是:

我到底需要在哪里做什么?

  1. MainActivity處理選項卡的創建
  2. BluetoothTab擴大了bluetooth_tab.xml的布局
  3. 圖形使graph_tab.xml的布局膨脹

我現在的問題是我不知道將藍牙連接的東西放在哪里。 是否將其放入MainActivity?

如果確實將藍牙連接的東西放在MainActivity中,如何從BluetoothTab片段中使用它?

有人可以用一個通常如何完成的示例來指出正確的方向嗎?

這里有一些代碼:

主要活動

package com.bavilo.braumeister;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    /* Declaring Your View and Variables */

    Toolbar toolbar;
    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[] = {"Bluetooth","Graph"};
    int Numboftabs = 2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Creating The Toolbar and setting it as the Toolbar for the activity
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch(id) {
            case R.id.menu_info:

                new AlertDialog.Builder(this).setTitle("About").setMessage("Hello").show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}

BluetoothTab

package com.bavilo.braumeister;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

public class BluetoothTab extends Fragment {

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bluetooth_tab,container,false);
        text = (TextView) v.findViewById(R.id.text);
        return v;
    }
}

圖形

package com.bavilo.braumeister;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Tab2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_2,container,false);
        return v;
    }
}

所有與BT連接相關的邏輯絕對應該進入MainActivity因為兩個選項卡都將使用它。 最好的方法是定義MainActivity將實現的回調接口,該接口將允許Fragments訪問BT相關功能。

保持“ Fragments ”中的邏輯最小。 例如:第一個Fragment只會使用按鈕注冊OnClickListener ,並調用MainActivity的回調方法之一(此方法將啟動BT連接)。

涉及的步驟與此處說明的步驟非常相似: http : //developer.android.com/training/basics/fragments/communicating.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM