按下时如何突出显示文本

时间:2017-01-27 14:46:28

标签: java android controls

我尝试编写一个简单的代码来在按下文本时更改文本颜色。 有人可以告诉我这段代码有什么问题吗?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

    TextView text = (TextView) findViewById(R.id.textView2);
    text.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public void OnClick(View v) {
            text.setTextColor(Color.GREEN);
        }

    }
}

4 个答案:

答案 0 :(得分:0)

您正在text onCreate之外或之前初始化Exception,而listener执行之前或之前无法设置oncreate

1。)将文本声明为全局

2。)在oncreate

中初始化并设置监听器
public class MainActivity extends AppCompatActivity {

   TextView text ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.textView2);
        text.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
               text.setTextColor(Color.GREEN);    
                return true;
            }
        });
    }
}

答案 1 :(得分:0)

在Android中添加onTouchListener,您需要实施onTouch

 text.setOnTouchListener(new View.OnTouchListener() {
            @Override public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

您也可以更改为OnClickListener,以便您的代码为:

public class MainActivity extends AppCompatActivity {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text = (TextView) findViewById(R.id.textView2);
        text.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                text.setTextColor(Color.GREEN);
            }
        });
    }
}

OnClickListener - 用于基本用途,当我们只需要用户点击时,即点击按钮,屏幕上没有拖动或手势

OnTouchListener - 它对屏幕上的按下/释放执行和操作它允许用户在屏幕上结合移动,向下触摸,向上触摸,手指拖动或任何移动手势

答案 2 :(得分:0)

这应该有效:

   text.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            text.setTextColor(Color.GREEN);
            return false;
        }
    });

这也应该在onCreate中这样:

public class MainActivity extends AppCompatActivity {

private TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.text);
    text.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            text.setTextColor(Color.GREEN);
            return false;
        }
    });
}
}

但我认为你混淆了onTouchListener和onClickListener。对于仅点击,我建议使用onClickListener。但你几乎就在那里,你把xD混合起来。

使用onClickListener

public class MainActivity extends AppCompatActivity {
private TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.text);
    text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            text.setTextColor(Color.GREEN);
        }
    });
}
}

修改

要切换颜色,您可以使用此颜色:

private TextView text;
private boolean toggle = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.text);
    text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            changeColor(toggle);
        }
    });
}

private void changeColor(boolean toggle){
    if(toggle){
        text.setTextColor(Color.BLACK);
    }else{
        text.setTextColor(Color.GREEN);
    }
    this.toggle = !toggle;
}

答案 3 :(得分:0)

几乎你是对的

text.setOnTouchListener(new View.OnTouchListener() {        
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // PRESSED
                text.setTextColor(Color.GREEN);
                return true; // if you want to handle the touch event
            case MotionEvent.ACTION_UP:
                // RELEASED
                text.setTextColor(Color.RED);
                return true; // if you want to handle the touch event
        }
        return false;
    }
});

但我建议你使用比这更有效的动画 检查 - http://www.androidhive.info/2013/06/android-working-with-xml-animations/https://www.tutorialspoint.com/android/android_animations.htm