非常小白的开始了安卓的编程之旅,由于没有系统的理解便开始直接码代码,而coursera上老师用的是老版本的IDE,而我自己装的eclipse是新版的IDE,提倡fragment写法,因此遇到不少问题,对于fragment的理解也在入门。
老师的activity是直接在mainactivity中便能够通过activity_main.xml便可以自动生成代码。但是由于在新版的IDE中提倡使用fragment,fragment可以理解为main_activity的子单元,main_activity由多个fragment构成,因此main_activity中只要写入多个fraagment的关系就可以了,而具体的布局在fragment.xml中进行编写。既然有更小的单位可以进行处理,自然在main_activity中的代码便简介了很多,更多的功能在fraagment中写。而粗略看了一下fragment的理解,可以把其理解为在activity中不同的view。jacaranda文件中会自动生成fragment中的后台代码部分,编写的后台响应便应该在这一部分进行覆盖。
public static class PlaceholderFragment extends Fragment
要想通过按钮添加一个打开浏览器和指定网页的操作,在fargment部分的代码如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mybutton = (Button) rootView.findViewById(R.id.button1);
mybutton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String url = "www.baidu.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
//i.setClass(getActivity(), new Activity().getClass());
//getActivity();
startActivity(i);
}
});
return rootView;
}
这样便通过button打开了百度的网页,可以看到xml部分代码如下:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="96dp"
android:text="@string/showgem" />
布局的样式如下: