找到你要的答案

Q:Custom TextView not refreshed only on phone

Q:自定义TextView不刷新仅在手机

I created a custom TextView that is displayed in a GridView. When the user taps on the TextView, a black circle has to be displayed in the center of this TextView. Actually this used to work fine on my tablet (an ASUS TF300, something like that). FYI all clicked textviews are then saved in a local database and can be loaded again.

I also tested that on a smartphone. Weird thing : the black circle is not displayed after a tap. However I know that it works because the black circles are well displayed if I save my UI and reload it again.

Did I miss something ?

Please find the code of the custom TextView below.

public class ChordItemTextView extends TextView {

// Properties
private ChordItem item;
public void setChordItem(ChordItem item)
{
    this.item = item;
}

private Paint fretPaint;
private Paint chordPaint;
private Paint textPaint;
private int paperColor;

public ChordItemTextView(Context context) {
    super(context);
    this.Init(context);
}

public ChordItemTextView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    this.Init(context);
}

private void Init(Context context)
{
    // Resources retrieval
    Resources myResources  = getResources();

    this.fretPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.fretPaint.setColor(myResources.getColor(R.color.fretColor));

    this.chordPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.chordPaint.setColor(myResources.getColor(R.color.fretColor));

    this.textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.textPaint.setColor(myResources.getColor(R.color.fretIndication));
    this.textPaint.setTextSize(myResources.getDimension(R.dimen.fretFontSize));

    this.paperColor = myResources.getColor(R.color.bgColor);
}

@Override
protected void onDraw(Canvas canvas) {

    // paper color
    canvas.drawColor(this.paperColor);

    // fret line drawing
    canvas.drawLine(0, getMeasuredHeight() / 2, getMeasuredWidth(), getMeasuredHeight() / 2, this.fretPaint);
    canvas.drawLine((float) (getMeasuredWidth() * 0.99), 0, (float) (getMeasuredWidth() * 0.99), getMeasuredHeight(), this.fretPaint);
    if (this.item.getFretNumber() == 0)
    {
        canvas.drawLine((float)( getMeasuredWidth() * 0.95), 0, (float)( getMeasuredWidth() * 0.95), getMeasuredHeight(), this.fretPaint);
    }

    // "tap" drawing
    if (this.item.getIsPushed())
    {
        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, getMeasuredHeight() / 4, this.chordPaint);
    }

    // fret number drawing
    if (this.item.getStringNumber() == 1)
    {
        canvas.drawText(String.valueOf(this.item.getFretNumber()), getMeasuredWidth() / 2, getMeasuredHeight(), this.textPaint);
    }

    super.onDraw(canvas);
}

}

我创建了一个自定义的方法,是在一个GridView显示。当用户点击TextView,黑色的圆圈必须在这个TextView展示中心。其实这用在我的平板电脑(华硕TF300精细的工作,像这样的东西)。还有所有的点击TextViews然后保存在本地数据库中,可以重新加载。

我还测试了在智能手机。怪异的东西:黑色圆圈不显示在一个水龙头。然而,我知道它的工作原理,因为黑眼圈很好地显示,如果我保存我的UI和重新加载它。

我错过什么了吗?

请找到下面的自定义TextView代码。

public class ChordItemTextView extends TextView {

// Properties
private ChordItem item;
public void setChordItem(ChordItem item)
{
    this.item = item;
}

private Paint fretPaint;
private Paint chordPaint;
private Paint textPaint;
private int paperColor;

public ChordItemTextView(Context context) {
    super(context);
    this.Init(context);
}

public ChordItemTextView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    this.Init(context);
}

private void Init(Context context)
{
    // Resources retrieval
    Resources myResources  = getResources();

    this.fretPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.fretPaint.setColor(myResources.getColor(R.color.fretColor));

    this.chordPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.chordPaint.setColor(myResources.getColor(R.color.fretColor));

    this.textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.textPaint.setColor(myResources.getColor(R.color.fretIndication));
    this.textPaint.setTextSize(myResources.getDimension(R.dimen.fretFontSize));

    this.paperColor = myResources.getColor(R.color.bgColor);
}

@Override
protected void onDraw(Canvas canvas) {

    // paper color
    canvas.drawColor(this.paperColor);

    // fret line drawing
    canvas.drawLine(0, getMeasuredHeight() / 2, getMeasuredWidth(), getMeasuredHeight() / 2, this.fretPaint);
    canvas.drawLine((float) (getMeasuredWidth() * 0.99), 0, (float) (getMeasuredWidth() * 0.99), getMeasuredHeight(), this.fretPaint);
    if (this.item.getFretNumber() == 0)
    {
        canvas.drawLine((float)( getMeasuredWidth() * 0.95), 0, (float)( getMeasuredWidth() * 0.95), getMeasuredHeight(), this.fretPaint);
    }

    // "tap" drawing
    if (this.item.getIsPushed())
    {
        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, getMeasuredHeight() / 4, this.chordPaint);
    }

    // fret number drawing
    if (this.item.getStringNumber() == 1)
    {
        canvas.drawText(String.valueOf(this.item.getFretNumber()), getMeasuredWidth() / 2, getMeasuredHeight(), this.textPaint);
    }

    super.onDraw(canvas);
}

}

android  textview