Android Studio配置Android annotation(注解)

配置说明:1.在project  buildgradle

dependencies {
    classpath classpath }

2. app的 build.gradle :

//配置Android annotations 

  apply : AAVersion = apt {
    {
        .outputs[]?.processResources?.manifestFile
    }
}
dependencies {
    compile fileTree(: , : [])
    testCompile compile compile apt $AAVersioncompile $AAVersion}
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath classpath }
}

     多重构几次即可

    对应的Activity   一定在Android Manifest中后面加个下划线。比如:

<activity android:name=".MainActivity_">

附小的demo:

1、布局文件.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"
    tools:context="mstart.yeketech.com.frescodemo.MainActivity">

    <EditText
        android:id="@+id/myInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:layout_below="@+id/myInput"
        android:id="@+id/myButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click me!"
        />
    <TextView
        android:layout_below="@+id/myButton"
        android:id="@+id/myTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    </RelativeLayout>

2、Activity

import android.app.Activity;
import android.widget.EditText;
import android.widget.TextView;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
    @ViewById(R.id.myInput)
    EditText myInput;

    @ViewById(R.id.myTextView)
    TextView textView;

    @Click
    void myButton() {
        String name = myInput.getText().toString();
        textView.setText("Hello "+name);
    }

/**
 * 不使用注解代码
 */
//    private Button myButton;
//    private TextView textView;
//    private EditText myInput;
//    @Override
//    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
//        super.onCreate(savedInstanceState, persistentState);
//        setContentView(R.layout.activity_main);
//        textView= (TextView) findViewById(R.id.myTextView);
//        myInput= (EditText) findViewById(R.id.myInput);
//        myButton= (Button) findViewById(R.id.myButton);
//        myButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String name = myInput.getText().toString();
//                textView.setText("Hello "+name);
//            }
//        });
//    }

}

3、效果图:

172607_HufK_2369789.png

4、注:一对比 就发现使用注解代码少很多;但是没有教扎实的基础,不建议使用注解;

 

 

转载于:https://my.oschina.net/MStart/blog/663167

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值