1

I am struggling with the logic behind this, so any tips would be appreciated. I have two classes, one in which the fragment is created and one in which my custom view is created, and I am trying to do some animation. In the main layout, I have an edit text field and a button. What I want to do is to add to the custom view when a button is clicked, the text within the edit text will be added to the custom view. Thinking about HOW to do this, I am drawing a blank, and I am beginning to think it is not possible. Should I just create the edit text within the custom view? Here is the code showing what I am doing (but stuck on a next step, or whether or not I should scrap this approach and try a different one)

The main fragment

public class DestroyerView extends Fragment
{



    private Context mContext;
    Paint paint = new Paint();
    private AnimatedNegative PositiveAnimatedNegative;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        mContext = this.getActivity();
        View view = inflater.inflate(R.layout.activity_destroyer, container, false);
        final Button fire = (Button) view.findViewById(R.id.destroy);
        PositiveAnimatedNegative = (AnimatedNegative) view.findViewById(R.id.anim_view);
        fire.setOnClickListener(new OnClickListener()
            {
            @Override
            public void onClick(View arg0) 
                {
                // logic here?
                }
            });
       return view;

    }
}


public AnimatedNegative(Context context, AttributeSet attrs)  

{

        super(context, attrs);


        mContext = this.getContext();

        h = new Handler();
        mCalendarDbHelper=new CalendarDbAdapter(mContext);
        mCalendarDbHelper.open();


}    

    private Runnable r= new Runnable() 
    {
    @Override
    public void run() 
            {
        invalidate(); 
    }
    };



    protected void onDraw (Canvas canvas)
    {
     String word = "This is a sentence";
     paint.setColor(Color.parseColor("#1E90FF")); 
     paint.setStyle(Style.FILL); 
     canvas.drawPaint(paint); 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(20); 
     x = this.getWidth()/2;
     y = this.getHeight()/2;
     canvas.drawText(word, x, y, paint);
     }
   }

... Now this would be much easier if I could reference the button inside of the custom view, but it doesn't seem I can do that, or if I could simply add to the custom view inside of the main activity class, but it doesn't seem that I can do that either (not without creating a new canvas, which seems to be doing too much for what it is that I want to do (just add the words)). So, finally, is the way I am currently trying to do this a dead end? Any help would be appreciated.

4

1 回答 1

2

我想我明白了。我将使用在我的主要活动中定义的 AnimatedNegative 对象调用 invalidate,然后从编辑文本中传入单词和一个真值,以便我可以在视图中检查它并重绘。添加的代码如下所示:

在主要活动中:

        public void onClick(View arg0) 
            {
            PositiveAnimatedNegative.invalidate();
            PositiveAnimatedNegative.add = true;
            PositiveAnimatedNegative.positive_word = "This is a positive word";
            }

在自定义视图中:

            if (add == true)
            {
                canvas.drawText(positive_word, x,y, paint);
            }
于 2013-02-14T18:31:12.000 回答