總覺得不用框架直接開發比較繁瑣,java下有很多,我使用ssh,php下有很多,我使用ThinkPHP,Delphi下還沒有找到一個比較合適的,Android下呢?今天在網上搜索了一下,發現也是有很多不錯的框架的,比較了一下,發現兩個比較好用的:Afinal和xUtils。
Afinal:
http://github.com/yangfuhai/afinal
xUtils:
https://github.com/wyouflf/xUtils
xUtils是在Afinal上fork了一個分支。
這兩個框架都使用了注解方式,使用起來真的很方便:
-
完全注解方式就可以進行UI綁定和事件綁定
-
無需findViewById和setClickListener等
簡單的入門介紹:
Afinal:
http://www.oschina.net/p/afinal/
xUtils源起:
http://my.oschina.net/u/1171837/blog/147544
對框架感興趣的朋友可以試一下。
也感謝作者提供的好工具!
附上簡單的控件和事件注入示例:
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tvHello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btnHello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="68dp" android:layout_toRightOf="@+id/tvHello" android:text="Button" /> </RelativeLayout>
java:
package com.example.myxutils; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.TextView; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import com.lidroid.xutils.view.annotation.event.OnClick; public class MainActivity extends Activity { // xUtils的view注解要求必須提供id,以使代碼混淆不受影響。 @ViewInject(R.id.tvHello)TextView tvHello; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //在Activity中注入: ViewUtils.inject(this); //注入view和事件 tvHello.setText("Hello,Garfield !"); } @OnClick(R.id.btnHello) public void testButtonClick(View v) { // 方法簽名必須和接口中的要求一致 tvHello.setText("這是按鈕事件 !"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
代碼簡潔了好多!