三色阶梯
程序员文章站
2022-07-13 15:30:30
...
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_weight="2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="三色梯"
android:textSize="24dp"
android:layout_weight="7"/>
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_weight="2"/>
</LinearLayout>
<wanghuiqi.bawie.com.threecolor_jieti.LadderView
android:id="@+id/lad_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
自定义控件LadderView
package wanghuiqi.bawie.com.threecolor_jieti;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class LadderView extends ViewGroup {
public LadderView(Context context) {
this(context,null);
}
public LadderView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public LadderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
int startWidth = 0;
int startHeight = 0;
for (int i=0; i<count; i++){
View v = getChildAt(i);
v.layout(startWidth,startHeight,startWidth+v.getMeasuredWidth(),startHeight+v.getMeasuredHeight());
if ((i+1)%3==0) {
startWidth=0;
}else {
startWidth += v.getMeasuredWidth();
}
startHeight += v.getMeasuredHeight();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
MainActivity
package wanghuiqi.bawie.com.threecolor_jieti;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import static java.security.AccessController.getContext;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_delete;
private Button btn_add;
private LadderView lad_view;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn_delete = (Button) findViewById(R.id.btn_delete);
btn_add = (Button) findViewById(R.id.btn_add);
btn_delete.setOnClickListener(this);
btn_add.setOnClickListener(this);
lad_view = (LadderView) findViewById(R.id.lad_view);
lad_view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
i++;
final TextView text = new TextView(this);
text.setWidth(250);
text.setHeight(100);
text.setText(" 条目" + i);
text.setTextColor(Color.WHITE);//字体颜色白色
if ((i + 1) % 3 == 0) {
text.setBackgroundColor(Color.RED);//背景颜色绿色2
} else if ((i + 1) % 3 == 1) {
text.setBackgroundColor(Color.GREEN);//背景颜色蓝色3
} else if ((i + 1) % 3 == 2) {
text.setBackgroundColor(Color.BLUE);//背景颜色红色1
}
lad_view.addView(text);
//长按删除选中条目
text.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("删除条目");
builder.setMessage("请确认是否要删除选中的条目");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
lad_view.removeView(text);
}
});
builder.setNegativeButton("取消", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
}
});
//点击删除(-)
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lad_view.removeView(text);
}
});
//点击条目跳转页面
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳跳跳", Toast.LENGTH_SHORT).show();
}
});
}
}