我有一个textview,我在其上应用了一个缩放缩放手势,可以在缩放时增大/减小文本视图的大小。我也想在它上面应用onclick监听器,但是当我应用onclick不工作时。我在互联网上搜索但没有找到解决方案。任何帮助将不胜感激。
我的代码:
package com.zeeshanfirstapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("ACtion", "Clicked");
}
});
final ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.OnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float size = textView.getTextSize();
float factor = detector.getScaleFactor();
float product = size*factor;
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
textView.invalidate();
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
});
View.OnTouchListener listener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return scaleGestureDetector.onTouchEvent(event);
}
};
textView.setOnTouchListener(listener);
}
}

答案 0 :(得分:0)
您应该在onCreateView中执行视图调整,因为在调用onCreate()时可能无法完全创建视图。