1、 用真机测试apk:
1. 设置手机,勾选USB调试:
各手机设置方法不一样,魅族mx2在设置-->辅助功能-->开发人员工具-->USB调试,将开关打开。
2、安装驱动:
这步折腾了比较久,都没找到能用的驱动,最后发现了个比较简单的办法:在PC端安装个豌豆荚,当手机连接到电脑后,豌豆荚会自动匹配驱动安装,
待安装完后再卸载了豌豆荚。
(可用adb devices查看设备列表,若驱动安装成功,则能看到设备)
3、设置工程的运行配置:
在工程中单击鼠标右键,依次选择选择: Run As-->Run Configurations-->Target-->Launch on all compatible devices/AVD's
2. activity_main.xml和fragment_main.xml怎么关联到代码中:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
3. ToDoList示例:
1. 按默认步骤创建一个名为ToDoList的Android Application Project工程;
2. 更改fragment_main.xml的布局方式为LinearLayout:
1. 打开fragmen_main.xml(Graphical Layout);
2. 在RelativeLayout上单击右键,选择Change Layout,将布局改为LinearLayout(Veritical);
3. 在fragment_main.xml中添加一个EditText和一个ListView,代码如下:
4. 在strings.xml中添加有名为addItemHint的String类型:
New To Do Item
5. 在MainActivity.java中添加如下代码:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView myListView = (ListView)rootView.findViewById(R.id.myListView);
final EditText myEditText = (EditText)rootView.findViewById(R.id.myEditText);
final ArrayList todoItems = new ArrayList();
final ArrayAdapter aa;
aa = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, todoItems);
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER ||
keyCode == KeyEvent.KEYCODE_ENTER)
{
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
}
return false;
}
});
return rootView;
}
注意这段代码的添加位置,是在PlaceholderFragment对象中。
6. apk完成,可将apk下载手机进行调试。