Android(安卓)Studio插件Android(安卓)Code Generator的使用
16lz
2021-01-26
一.插件安装
command+逗号,打开设置界面,左边栏搜索Plugins,点击Browse repositories...
Snip20170118_6.png搜索Android Code Generator,点击安装,重启Android Studio即可
Snip20170118_7.png一.插件使用
首先准备好布局文件activity_text.xml
<?xml version="1.0" encoding="utf-8"?>
在布局文件layout中选中布局,右击,选择Android Code Generator
Snip20170118_9.png从上图可以看出,可以自动生成Activity、Adapter和Fragment
也可以和ButterKnife结合使用。
自动生成Activity
原始的findViewById
a.gif自动生成代码
package com.zhoujian.butterknife;import android.os.Bundle;import android.app.Activity;import android.widget.TextView;public class ActivityTextActivity extends Activity { private TextView text1; private TextView text2; private TextView text3; private TextView text4; private TextView text5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text); text1 = (TextView) findViewById(R.id.text1); text2 = (TextView) findViewById(R.id.text2); text3 = (TextView) findViewById(R.id.text3); text4 = (TextView) findViewById(R.id.text4); text5 = (TextView) findViewById(R.id.text5); }}
和ButterKnife结合使用
b.gif自动生成代码
package com.zhoujian.butterknife;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;import butterknife.BindView;import butterknife.ButterKnife;public class ActivityTextActivity extends Activity { @BindView(R.id.text1) TextView text1; @BindView(R.id.text2) TextView text2; @BindView(R.id.text3) TextView text3; @BindView(R.id.text4) TextView text4; @BindView(R.id.text5) TextView text5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text); ButterKnife.bind(this); }}
注意:和ButterKnife结合使用时候,@Bind要改成@BindView
自动生成Adapter
package com.zhoujian.butterknife;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import butterknife.BindView;import butterknife.ButterKnife;import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;public class ActivityTextAdapter extends BaseAdapter { private List objects = new ArrayList(); private Context context; private LayoutInflater layoutInflater; public ActivityTextAdapter(Context context) { this.context = context; this.layoutInflater = LayoutInflater.from(context); } @Override public int getCount() { return objects.size(); } @Override public T getItem(int position) { return objects.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = layoutInflater.inflate(R.layout.activity_text, null); convertView.setTag(new ViewHolder(convertView)); } initializeViews((T)getItem(position), (ViewHolder) convertView.getTag()); return convertView; } private void initializeViews(T object, ViewHolder holder) { //TODO implement } static class ViewHolder { @BindView(R.id.text1) TextView text1; @BindView(R.id.text2) TextView text2; @BindView(R.id.text3) TextView text3; @BindView(R.id.text4) TextView text4; @BindView(R.id.text5) TextView text5; ViewHolder(View view) { ButterKnife.bind(this, view); } }}
注: private List
自动生成Fragment
package com.zhoujian.butterknife;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.Unbinder;public class ActivityTextFragment extends Fragment { @BindView(R.id.text1) TextView text1; @BindView(R.id.text2) TextView text2; @BindView(R.id.text3) TextView text3; @BindView(R.id.text4) TextView text4; @BindView(R.id.text5) TextView text5; private Unbinder mBind; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.activity_text, null); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mBind = ButterKnife.bind(this, view); } @Override public void onDestroyView() { super.onDestroyView(); mBind.unbind(); }}
更多相关文章
- android学习笔记5--------------业务bean(单元测试) .
- 友盟统计 集成测试看不到实时日志
- Android(安卓)如何更改包名
- 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(一)
- [Android]读写fb0测试
- android.database.sqlite.SQLiteConstraintException: error cod
- TextView和EditText中添加图片(ImageSpan)
- Android开发指南(35) —— Toast Notifications
- Android(安卓)PullToRefresh 详解