2013-04-26 48 views
0

我在制作一个只显示时钟的应用程序,但我希望每次用户触摸屏幕时都会更改文本的颜色(来自在colors.xml文件中的预选颜色列表),但我没有线索从哪里开始。有人能指引我朝着正确的方向吗?如何更改触摸(屏幕)上的textView(digitalClock)颜色

这里的主要活动:

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); 
} 

// Make the handler subclass static because of this: http://stackoverflow.com/a/11408340/111777 
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(350 - 100); 
     int y = RANDOM.nextInt(800 - 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); 
    } 
} 

private static final String TAG = MainActivity.class.getSimpleName(); 

}

和这里的布局XML:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
tools:context=".MainActivity" 
android:background="@color/black" > 

<DigitalClock 
    android:id="@+id/digitalClock1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="DigitalClock" 
    android:textColor="@color/ics_blue" 
    android:textSize="28sp" /> 

回答

0

我还没有DigitalClock做交易,但我认为,首先,你应该参考DigitalClock variable,n ot TextView。其次,要拦截触摸事件,您需要覆盖您的活动的方法,每次用户触摸屏幕时都会回调。

0

您应该遵循以下步骤

  1. 使用一个TimerTask to.continusly显示时间
  2. 对时钟视图实现一个touchlistener

这样 view.setOnTouchListener

  1. 制作数据像这样的颜色

int [] colr = {Color.BLACK,Color.BLUE};

并在您的触摸事件中使用随机索引并将其设置为您视图的颜色