public class MainActivity extends AppCompatActivity{
private BeanView bview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bview = findViewById(R.id.bview);
bview.setOnBeanClickListener(new BeanView.onBeanClickListener() {
@Override
public void onBeanClick(View v) {
Toast.makeText(MainActivity.this,"sssssaasfjanljknalkjn",Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(MainActivity.this)
.setMessage("需要上传头像")
.create().show();
}
@Override
public void onBean(View v) {
Toast.makeText(MainActivity.this,"kkk",Toast.LENGTH_LONG).show();
new AlertDialog.Builder(MainActivity.this)
.setMessage("需要更改内容")
.create().show();
}
});
}
}
public class BeanView extends LinearLayout implements View.OnClickListener {
public interface onBeanClickListener{
void onBeanClick(View v);
void onBean(View v);
}
private onBeanClickListener listener;
public void setOnBeanClickListener(onBeanClickListener listener) {
this.listener = listener;
}
private ImageView image_logo;
private TextView textName;
private Context context;
public BeanView(Context context) {
super(context);
initView(context);
}
public BeanView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public BeanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
this.context = context;
View.inflate(context,R.layout.view_layout,this);
textName = findViewById(R.id.textName);
image_logo = findViewById(R.id.image_logo);
textName.setOnClickListener(this);
image_logo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.image_logo:
listener.onBeanClick(this);
break;
case R.id.textName:
listener.onBean(this);
break;
}
}
}
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"/>
</LinearLayout>
mainxml
<LinearLayout 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="com.bwie.wang.MainActivity">
<com.bwie.lri.BeanView
android:id="@+id/bview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></com.bwie.lri.BeanView>
</LinearLayout>