我在一项活动中需要一些输入来制作可绘制的形状。这是它的一个小例子:
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.OVAL);
gd.setColor(Color.RED);
gd.setCornerRadius(12);
preview.getLayoutParams().width = 100;
preview.getLayoutParams().height = 100;
preview.setBackground(gd);
但是当我想将它实现到微调器中时,我必须多次选择所有选项,这样我才能得到结果。还有一个按钮应该显示预览,但不起作用。
这是我的代码:
import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import yuku.ambilwarna.AmbilWarnaDialog;
public class CustomizationActivity extends AppCompatActivity {
int currentColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customization);
final EditText widthText = findViewById(R.id.width);
final EditText heightText = findViewById(R.id.height);
final Spinner shapeSpinner = findViewById(R.id.shape);
final View colorView = findViewById(R.id.color);
final View preview = findViewById(R.id.preview);
final EditText size = findViewById(R.id.size);
final EditText corners = findViewById(R.id.corners);
Button button = findViewById(R.id.button);
final GradientDrawable gd = new GradientDrawable();
shapeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
gd.setShape(GradientDrawable.RECTANGLE);
gd.setCornerRadius(Integer.parseInt(corners.getText().toString()));
preview.getLayoutParams().width = Integer.parseInt(widthText.getText().toString());
preview.getLayoutParams().height = Integer.parseInt(heightText.getText().toString());
}
if (position == 2) {
gd.setShape(GradientDrawable.OVAL);
preview.getLayoutParams().width = Integer.parseInt(size.getText().toString());
preview.getLayoutParams().height = Integer.parseInt(size.getText().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
colorView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(CustomizationActivity.this, currentColor, false, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
currentColor = color;
colorView.setBackgroundColor(color);
gd.setColor(color);
}
});
dialog.show();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
preview.setBackground(gd);
}
});
}
}
它以某种方式工作,但我不知道如何纠正它。我想要的是 - 如果可能的话 - 在值更改时直接显示预览。- 如果没有,在编辑完所有内容后,点击按钮获得结果。
我不知道,但我认为问题出在微调器上。
请问有什么帮助吗?