Android全埋点解决方案
上QQ阅读APP看书,第一时间看更新

1.1 Android View类型

在Android系统中,控件(View)的类型非常丰富。分类方式也是多种多样的。我们根据控件设置的监听器(listener)的不同,可以大致将控件分为如下几类。

·Button、CheckedTextView、TextView、ImageButton、ImageView等

为这些控件设置的listener均是android.view.View.OnClickListener。

下面以Button为例:

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //do something
    }
});
SeekBar

·SeekBar

SeekBar设置的listener是android.widget.SeekBar.OnSeekBarChangeListener,如:

SeekBar seekBar = findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        // do something
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // do something
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // do something
        }
});

·TabHost

TabHost设置的listener是android.widget.TabHost.OnTabChangeListener,如:

TabHost tabHost = findViewById(R.id.tabhost);
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabName) {
        //do something
    }
});

·RatingBar

RatingBar设置的listerner是android.widget.RatingBar.OnRatingBarChangeListener,如:

RatingBar ratingBar = findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(newRatingBar.OnRatingBarChangeListener() { 
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        //do something
    }
});

·CheckBox、SwitchCompat、RadioButton、ToggleButton、RadioGroup等

这些View属于同一种类型,它们都是属于带有“状态”的按钮,它们设置的listener均是CompoundButton.OnCheckedChangeListener。

下面以CheckBox为例:

CheckBox checkBox = findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){ 
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        //do something
    } 
});

·Spinner

Spinner设置的listener是android.widget.AdapterView.OnItemSelectedListener,如:

Spinner spinner = findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //do something
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    } 
});

·MenuItem

主要是通过重写Activity的相关方法(onOptionsItemSelected、onContextItemSelected)来设置listener,如:

//选项菜单
@Override
public boolean onOptionsItemSelected(android.view.MenuItem) {
    //do something
}

//上下文菜单
@Override
public boolean onContextItemSelected(android.view.MenuItem) {
    //do something
}

·ListView、GridView

ListView和GridView都是AdapterView的子类,显示的内容都是一个“集合”。它们设置的listener均是android.widget.AdapterView.OnItemClickListener,如:

ListView listView = findViewById(R.id.listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //do something
    }
});

·ExpandableListView

ExpandableListView也是AdapterView的子类,同时也是ListView的子类。它的点击分为ChildClick和GroupClick两种情况,所以,它设置的listener也是分为两种情况,即:android.widget.ExpandableListView.OnChildClickListener和android.widget.ExpandableList-View.OnGroupClickListener,如:

ExpandableListView listview = findViewById(R.id.expandablelistview);
//ChildClick
listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView expandableListView, 
                        View view, int groupPosition, int childPosition, long id) {
        //do something
        return true; 
    }
});

//GroupClick
listview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, 
                    View view, int childPosition, long id) {
        //do something
        return true; 
    }
});

·Dialog

Dialog设置的listener分为两种情况。对于常见的普通Dialog,设置的listener是android.content.DialogInterface.OnClickListener,如:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //do something
    } 
});

还有一种是显示列表的Dialog,它设置的listener是android.content.DialogInterface.OnMultiChoiceClickListener,如:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
DialogInterface.OnMultiChoiceClickListener mutiListener =
                            new DialogInterface.OnMultiChoiceClickListener() {

    @Override
    public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {
        //do something
    }
};