我试图列出一个文本视图来创建文本视图,并且创建了一个文本视图,但是当移动它们时,只有第一个文本视图会移动,其余的不会。当我单击A按钮时,它将创建文本视图,但是只有第一个移动并选择特定的textview来执行诸如编辑,大小等操作。
代码在下面发布。以下是完整的活动代码。
public class Home extends AppCompatActivity {
ConstraintLayout imageConst, itemConst, stylingConst;
Toolbar toolBar;
float x_cord;
float y_cord;
ImageView imageView;
ImageButton imageButton, textButton;
Button colorButton, sizeButton, fontButton, removeButton, editButton,
rotateButton;
int id = 0;
Vibrator vibrator;
public ArrayList<Drawable> imageList;
private RecyclerView recyclerView;
TextView textArray[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
imageConst = findViewById(R.id.home_imageconst);
imageView = findViewById(R.id.home_image);
itemConst = findViewById(R.id.home_itemconst);
toolBar = findViewById(R.id.home_toolbar);
imageButton = findViewById(R.id.home_imagebutton);
textButton = findViewById(R.id.home_textbutton);
recyclerView = findViewById(R.id.home_recycler);
stylingConst = findViewById(R.id.home_stylingconst);
vibrator = (Vibrator)
this.getSystemService(Context.VIBRATOR_SERVICE);
toolBar.setTitle("Writers Den");
colorButton = findViewById(R.id.home_textcolor);
sizeButton = findViewById(R.id.home_textsize);
fontButton = findViewById(R.id.home_textfont);
removeButton = findViewById(R.id.home_textremove);
editButton = findViewById(R.id.home_edittext);
rotateButton = findViewById(R.id.home_textrotate);
textArray = new TextView[5];
makeList();
stylingConst.setVisibility(View.INVISIBLE);
selectImage(2);
createText(id, "Text");
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.setVisibility(View.VISIBLE);
loadImages();
}
});
imageConst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stylingConst.setVisibility(View.INVISIBLE);
itemConst.setVisibility(View.VISIBLE);
}
});
textButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createTextBuilder();
}
});
/////Move & decoration///////////////////
if (textArray[0] != null) {
textArray[0].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
vibrator.vibrate(50);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x_cord = v.getX() - event.getRawX();
y_cord = v.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
v.animate()
.x(event.getRawX() + x_cord)
.y(event.getRawY() + y_cord)
.setDuration(0)
.start();
break;
default:
return false;
}
return false;
}
});
textArray[0].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stylingConst.setVisibility(View.VISIBLE);
itemConst.setVisibility(View.INVISIBLE);
}
});
}
if (textArray[1] != null) {
textArray[1].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
vibrator.vibrate(50);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x_cord = v.getX() - event.getRawX();
y_cord = v.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
v.animate()
.x(event.getRawX() + x_cord)
.y(event.getRawY() + y_cord)
.setDuration(0)
.start();
break;
default:
return false;
}
return false;
}
});
textArray[1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stylingConst.setVisibility(View.VISIBLE);
itemConst.setVisibility(View.INVISIBLE);
}
});
}
if (textArray[2] != null) {
textArray[2].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
vibrator.vibrate(50);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x_cord = v.getX() - event.getRawX();
y_cord = v.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
v.animate()
.x(event.getRawX() + x_cord)
.y(event.getRawY() + y_cord)
.setDuration(0)
.start();
break;
default:
return false;
}
return false;
}
});
textArray[2].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stylingConst.setVisibility(View.VISIBLE);
itemConst.setVisibility(View.INVISIBLE);
}
});
}
if (textArray[3] != null) {
textArray[3].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
vibrator.vibrate(50);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x_cord = v.getX() - event.getRawX();
y_cord = v.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
v.animate()
.x(event.getRawX() + x_cord)
.y(event.getRawY() + y_cord)
.setDuration(0)
.start();
break;
default:
return false;
}
return false;
}
});
textArray[3].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stylingConst.setVisibility(View.VISIBLE);
itemConst.setVisibility(View.INVISIBLE);
}
});
}
if (textArray[4] != null) {
textArray[4].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
vibrator.vibrate(50);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x_cord = v.getX() - event.getRawX();
y_cord = v.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
v.animate()
.x(event.getRawX() + x_cord)
.y(event.getRawY() + y_cord)
.setDuration(0)
.start();
break;
default:
return false;
}
return false;
}
});
textArray[4].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stylingConst.setVisibility(View.VISIBLE);
itemConst.setVisibility(View.INVISIBLE);
}
});
}
/////Move & decoration///////////////////
}
public void createTextBuilder() {
if (id < 5) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText editText = new EditText(this);
editText.setHint("Your words..");
editText.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(editText);
builder.setTitle("New text");
builder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setPositiveButton("Submit", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createText(id, editText.getText().toString());
}
});
builder.show();
} else {
Toast.makeText(this, "Maximum 5 Texts you can use",
Toast.LENGTH_SHORT).show();
}
}
private void createText(int index, String text) {
ConstraintSet set = new ConstraintSet();
set.clone(imageConst);
//New textview
textArray[index] = new TextView(this);
textArray[index].setText(text);
textArray[index].setId(id);
textArray[index].setClickable(true);
id = id + 1;
textArray[index].setGravity(Gravity.CENTER);
imageConst.addView(textArray[index]);
set.connect(textArray[index].getId(), ConstraintSet.TOP,
ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set.connect(textArray[index].getId(), ConstraintSet.BOTTOM,
ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set.connect(textArray[index].getId(), ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set.connect(textArray[index].getId(), ConstraintSet.LEFT,
ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
set.constrainHeight(textArray[index].getId(),
ConstraintSet.WRAP_CONTENT);
set.constrainWidth(textArray[index].getId(),
ConstraintSet.WRAP_CONTENT);
set.applyTo(imageConst);
}
public void selectImage(int i) {
recyclerView.setVisibility(View.INVISIBLE);
ConstraintSet set = new ConstraintSet();
set.clone(imageConst);
imageView.setImageDrawable(imageList.get(i));
}
private void loadImages() {
LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
linearLayoutManager.setStackFromEnd(false);
recyclerView.setLayoutManager(linearLayoutManager);
ImageAdapter imageAdapter = new ImageAdapter(this, imageList);
recyclerView.setAdapter(imageAdapter);
}
//////////////////////////////////////////////////////////////////////////////////
private void makeList() {
imageList = new ArrayList<Drawable>();
AssetManager assetManager = getAssets();
InputStream inputStream = null;
//1
try {
inputStream = assetManager.open("images/image1.jpg");
} catch (IOException e) {
e.printStackTrace();
}
Drawable drawable1 = Drawable.createFromStream(inputStream, "");
imageList.add(drawable1);
//2
try {
inputStream = assetManager.open("images/image2.jpg");
} catch (IOException e) {
e.printStackTrace();
}
Drawable drawable2 = Drawable.createFromStream(inputStream, "");
imageList.add(drawable2);
//3
try {
inputStream = assetManager.open("images/image3.jpg");
} catch (IOException e) {
e.printStackTrace();
}
Drawable drawable3 = Drawable.createFromStream(inputStream, "");
imageList.add(drawable3);
//4
try {
inputStream = assetManager.open("images/image4.jpg");
} catch (IOException e) {
e.printStackTrace();
}
Drawable drawable4 = Drawable.createFromStream(inputStream, "");
imageList.add(drawable4);
//5
try {
inputStream = assetManager.open("images/image5.jpg");
} catch (IOException e) {
e.printStackTrace();
}
Drawable drawable5 = Drawable.createFromStream(inputStream, "");
imageList.add(drawable5);
}
/////////////////////////////////////////////////////
}