我想创建一个扩展EditText的类,并且我从json文件传递height,width,textsize,xposition,yposition。 现在,我不知道如何设置它的背景颜色。我还想创建边框,如果在JSON文件中只有左侧和右侧底部的两个参数,则默认将两个参数的其余部分视为0并绘制边框。 。请帮帮我。
这是我的代码..
public EditText createEditText() throws JSONException
{
editText = new EditText(mContext);
LayoutParams layoutParams=new LayoutParams(convertToPixel(jsonObject.getInt("width")),convertToPixel(jsonObject.getInt("height")));
layoutParams.setMargins(convertToPixel(jsonObject.getInt("xposition")),convertToPixel(jsonObject.getInt("yposition")), 0, 0);
editText.setLayoutParams(layoutParams);
editText.setGravity(Gravity.TOP);
try
{
if(jsonObject.has("tips"))
{
editText.setHint(jsonObject.getString("tips"));
}
else{
editText.setHint("Enter Text");
}
if(jsonObject.has("textsize")){
editText.setTextSize(jsonObject.getInt("textsize"));
}else{
editText.setTextSize(14);
}
if(jsonObject.has("textcolor")){
editText.setTextColor(Color.parseColor(jsonObject.getString("textcolor")));
}else
{
editText.setTextColor(Color.BLACK);
}
if(jsonObject.has("topleftR") && jsonObject.has("toprightR") && jsonObject.has("bottomleftR") && jsonObject.has("bottomrightR"))
{
// top-left,top-right,bottom-right,bottom-left
int topleftR = convertToPixel(jsonObject.getInt("topleftR"));
int toprightR=convertToPixel(jsonObject.getInt("toprightR"));
int bottomleftR =convertToPixel(jsonObject.getInt("bottomleftR"));
int bottomrightR =convertToPixel(jsonObject.getInt("bottomrightR"));
ShapeDrawable mDrawable = new ShapeDrawable(new RoundRectShape(new float[]{
topleftR,topleftR,toprightR,toprightR,
bottomrightR,bottomrightR,bottomleftR,bottomleftR},
null, null));
mDrawable.getPaint().setColor(Color.parseColor(jsonObject.getString("bordercolor")));
mDrawable.getPaint().setStyle(Paint.Style.STROKE);
mDrawable.getPaint().setStrokeWidth(convertToPixel(3));
mDrawable.setBounds(convertToPixel(jsonObject.getInt("xposition")),convertToPixel(jsonObject.getInt("yposition")),
convertToPixel(jsonObject.getInt("xposition")) + convertToPixel(jsonObject.getInt("width")),
convertToPixel(jsonObject.getInt("yposition")) + convertToPixel(jsonObject.getInt("height")));
editText.setBackgroundDrawable(mDrawable);
}
else
{
editText.setBackgroundResource(android.R.drawable.editbox_background);
}
}
catch(NullPointerException e){}
//editText.setBackgroundColor(Color.YELLOW);
return editText;
}
答案 0 :(得分:0)
为什么要使用EditText?无论如何,您可以像这样设置背景:
editText.setBackground(R.drawable.your_image_here);
现在,如果你成功获得了json对象,那么
try{
String leftTop = jsonObject.getString("leftTopR");
String rightBottom = jsonObject.getString("rightBottomR");
} catch (JSONException e){
e.printtackTrace;
}
if (leftTop.equalsIgnoreCase("0") && rightBottom.equalsIgnoreCase("0"){
//// draw your border here
}