我对Android很新,但我想为未来的项目开发自己的应用程序。这个的基本思想是有两个标签(现在)。
第一个标签蓝牙将有一个按钮,用于在Android和蓝牙模块之间建立连接。
第二个选项卡Graph需要读取输入流并使用此数据绘制图表与时间的关系。
现在我的问题是:
我到底需要做什么?
我现在的问题是,我不知道在哪里放蓝牙连接的东西。我把它放在MainActivity中吗?
如果我将蓝牙连接内容放在MainActivity中,我如何在BluetoothTab片段中使用它?
有人能指出我正确的方向吗?可能会举例说明这通常是怎么做的?
这里有一些代码:
MainActivity
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;
}
}
答案 0 :(得分:0)
所有与BT连接相关的逻辑都应该进入MainActivity
,因为两个标签都将使用它。最好的方法是定义MainActivity
将实现的回调接口,这将允许Fragments
访问与BT相关的功能。
将Fragments
中的逻辑保持在最低限度。例如:第一个Fragment
只会使用按钮注册OnClickListener
,并调用MainActivity
的一个回调方法(此方法将启动BT连接)。
所涉及的步骤与此处说明的步骤非常相似:http://developer.android.com/training/basics/fragments/communicating.html