Android开发实战之与JAVA开发对比

来源:互联网 发布:北京炸酱面知乎 编辑:程序博客网 时间:2024/06/06 20:15

        随着移动互联网以及互联网+的逐步走俏,越来越多的企业开始进入移动互联网,想通过互联网分一杯羹,但是移动互联网开发成本还是很高的,比如iphone开发的审核机制漫长,可能无法满足快速的部署应用,而且开发人员相对较少,而中观中国移动之开发队伍,可以用十之八九都是android麾下,所以,为了快速融入互联网+的潮流,选择一个开发人员多,资源丰富,门槛低的开发平台才是明智之选,综上,我选择了Android作为进入移动平台的开发语言,那么说回来了,Android是基于JAVA进行开发的,那么针对于JAVA开发人员,Android又与Java开发有什么不同之处呢?是不是会JAVA开发就会安卓开发了呢?那么戴着这些问题,我们来进入Android的世界。。。。

       如果要对比Android和Java的区别,我们通过几个简单的例子来说起,我们通过一个简单的应用来介绍他们的关系,今日头条大家可能都不陌生,救我个人而言,我特别喜欢这个软件,我并不是在给这个软件做广告,确实是不错,很实用,也很适合我们拿来做例子学习人家的优秀设计,好了开始上图,如下是我弄的简单的效果,就是为了,让大家了解我接下来要干什么?

看看是不是很像,上面的导航条可以左右滑动,下面的列表单击后会有提示效果,这就是我们今天要距离说明的内容,好了,开始上代码。

下面第一段代码是运行的入口Activity也就是我们常说的主页面。

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TabHost;public class MainActivity extends ActionBarActivity {private String aaa = "ceasdasd";private ListView ListView = null;public String getAaa() {return aaa;}public void setAaa(String aaa) {this.aaa = aaa;}public ListView getListView() {return ListView;}public void setListView(ListView listView) {ListView = listView;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initTabHost();initListView();}public void initTabHost() {TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);tabHost.setup();tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("头条").setContent(R.id.listView1));tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("推荐").setContent(R.id.view2));tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("沈阳").setContent(R.id.view3));tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("社会").setContent(R.id.view4));tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator("娱乐").setContent(R.id.view5));tabHost.addTab(tabHost.newTabSpec("tab6").setIndicator("科技").setContent(R.id.view6));}public void initListView() {this.ListView = (ListView) findViewById(R.id.listView1);SimpleAdapter adapter = new SimpleAdapter(this, getData(),R.layout.simple_list, new String[] { "title" },new int[] { R.id.textView1 });this.ListView.setAdapter(adapter);this.ListView.setOnItemClickListener(new ListItemOnClickListenerImp(this));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}private List<Map<String, Object>> getData() {List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();Map<String, Object> map = new HashMap<String, Object>();map.put("title", "摩托罗拉");list.add(map);map = new HashMap<String, Object>();map.put("title", "诺基亚");list.add(map);map = new HashMap<String, Object>();map.put("title", "三星");list.add(map);map = new HashMap<String, Object>();map.put("title", "三星");list.add(map);map = new HashMap<String, Object>();map.put("title", "三星");list.add(map);map = new HashMap<String, Object>();map.put("title", "三星");list.add(map);map = new HashMap<String, Object>();map.put("title", "三星");list.add(map);map = new HashMap<String, Object>();map.put("title", "三星");list.add(map);map = new HashMap<String, Object>();map.put("title", "三星");list.add(map);map = new HashMap<String, Object>();map.put("title", "三星");list.add(map);return list;}}
接下来这一段代码是我单独写的一个事件类,为了代码的可读性,以及代码的结构性,和接下来要说的java使用方法是相结合的。

import java.util.Map;import android.app.AlertDialog;import android.app.Dialog;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;public class ListItemOnClickListenerImp implements OnItemClickListener {private MainActivity mainActivity;public ListItemOnClickListenerImp(MainActivity superMainActivity) {this.mainActivity = superMainActivity;}@SuppressWarnings("unchecked")@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Map<String, Object> map_obj=(Map<String, Object>) this.mainActivity.getListView().getItemAtPosition(arg2);Dialog alertDialog = new AlertDialog.Builder(this.mainActivity).setTitle("Message").setMessage(map_obj.get("title").toString()).setCancelable(true).create();alertDialog.show();}}
好了,现在开始说说研究结果,就是我们学到了什么?仔细看两段代码,是不是很熟悉,优雅的java代码,面向对象开发,继承,事件该有的都有了,俨然就是java开发,

但是他又和javaEE和java桌面应用有很大的不同,他是为了适应移动而生,所以继续看代码,总结如下:

1,android中的所有对象都可以通过findViewById(id)来获取,但是自定义的控件,不是安卓自动实现的情况下,Id的获取方式如下:android.R.ID.对象ID。

2,单独写得事件类需要传入的主Activity需要通过构造方法来传入,这样即可食用,其他主类的方法通过set和Get即可获取到,跟普通的面向对象开发基本一致。

以上两条是代码的总结,下面这段代码是页面布局文件的说明:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:ignore="MergeRootFrame" >    <TabHost        android:id="@android:id/tabhost"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical" >            <HorizontalScrollView                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:scrollbars="none" >                <TabWidget                    android:id="@android:id/tabs"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content" >                </TabWidget>            </HorizontalScrollView>            <FrameLayout                android:id="@android:id/tabcontent"                android:layout_width="fill_parent"                android:layout_height="fill_parent" >                  <ListView                    android:id="@+id/listView1"                    android:layout_width="fill_parent"                    android:layout_height="fill_parent" >                </ListView>                                <TableLayout                    android:id="@+id/view2"                    android:layout_width="fill_parent"                    android:layout_height="fill_parent" >                    <TableRow                        android:id="@+id/tableRow2"                        android:layout_width="fill_parent"                        android:layout_height="fill_parent" >                        <TextView                            android:id="@+id/textView2"                            android:layout_width="wrap_content"                            android:layout_height="wrap_content"                            android:gravity="right"                            android:text="密码" />                        <EditText                            android:id="@+id/editText2"                            android:layout_width="wrap_content"                            android:layout_height="wrap_content" />                    </TableRow>                    <TableRow                        android:id="@+id/tableRow3"                        android:layout_width="fill_parent"                        android:layout_height="fill_parent" >                        <Button                            android:id="@+id/button1"                            android:layout_width="wrap_content"                            android:layout_height="wrap_content"                            android:text="按钮2" />                        <Button                            android:id="@+id/button2"                            android:layout_width="wrap_content"                            android:layout_height="wrap_content"                            android:text="按钮1" />                    </TableRow>                </TableLayout>                <TextView                    android:id="@+id/view3"                    android:layout_width="fill_parent"                    android:layout_height="fill_parent" />                <TextView                    android:id="@+id/view4"                    android:layout_width="fill_parent"                    android:layout_height="fill_parent"                    android:text="diyige " />                <TextView                    android:id="@+id/view5"                    android:layout_width="fill_parent"                    android:layout_height="fill_parent" />                <TextView                    android:id="@+id/view6"                    android:layout_width="fill_parent"                    android:layout_height="fill_parent" />                          </FrameLayout>        </LinearLayout>    </TabHost></FrameLayout>
要想实现tab过多的左右移动效果,需要借助HorizontalScrollView来进行实现,我们只需要在TabWidget的外围添加上即可。

总结的很多,要写的也很多,因为工作原因,时间不是很多,今天就写到这里,明天还会继续完善,每天并不是简简单单完成任务,而是要通过工作的实际需求来

提高自己,所以要坚持写总结,将自己总结的经验分享给大家,同时也给自己留有备份,以备不时之需,同时当你真正写出来的时候,你才真是进行了相应的归纳总结

你才真正的进步,写得不好,刚刚入门,有不对的地方还希望牛人多多指点。



























0 0
原创粉丝点击