0

一直在做一个项目,现在我想要它,这样当用户触摸屏幕时,它会改变 textView(DigitalClock) 的颜色。谈到java,我有点菜鸟,所以如果可能的话,我真的需要一个完整的例子吗?到目前为止,这是我的代码:

public class MainActivity extends Activity {
private static final Random RANDOM = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);

    Handler handler = new RandomMoveHandler((TextView) findViewById(R.id.digitalClock1));
    handler.sendEmptyMessage(0);
}


private static class RandomMoveHandler extends Handler {
    private final WeakReference<TextView> textViewWeakReference;

    private RandomMoveHandler(TextView textView) {
        this.textViewWeakReference = new WeakReference<TextView>(textView);
    }

    @Override
    public void handleMessage(Message msg) {
        TextView textView = textViewWeakReference.get();
        if (textView == null) {
            Log.i(TAG, "WeakReference is gone so giving up.");
            return;
        }

        int x = RANDOM.nextInt(670 - 100);
        int y = RANDOM.nextInt(1230 - 100);

        Log.i(TAG, String.format("Moving text view to (%d, %d)", x, y));
        textView.setX(x);
        textView.setY(y);

        //change the text position here
        this.sendEmptyMessageDelayed(0, 30000);
    }
}
4

1 回答 1

0
textView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // use any of your own colors here...
        textView.setTextColor(android.R.color.holo_purple);
        return true;
    }
});

另请参阅https://developer.android.com/reference/android/widget/TextView.html#setTextColor(int)

于 2013-05-21T08:08:21.347 回答