Android-在自定义视图中从onTouch方法向mainActivity发送信息
我有一个自定义视图(DrawFigures),它有一个onTouch方法来执行一系列操作。有一个布局,它有几个按钮、一个TextView和一个RelativeView来动态添加自定义视图。然后,在MainActivity.class中,当在此自定义视图中调用onTouch方法时,我需要接收DrawFigures中某些属性的值,以便在TextView组件中显示它们 以下是自定义视图的代码:Android-在自定义视图中从onTouch方法向mainActivity发送信息,android,view,ontouch,Android,View,Ontouch,我有一个自定义视图(DrawFigures),它有一个onTouch方法来执行一系列操作。有一个布局,它有几个按钮、一个TextView和一个RelativeView来动态添加自定义视图。然后,在MainActivity.class中,当在此自定义视图中调用onTouch方法时,我需要接收DrawFigures中某些属性的值,以便在TextView组件中显示它们 以下是自定义视图的代码: public abstract class DrawFigures extends View{ pr
public abstract class DrawFigures extends View{
private float value1;
public boolean onTouch(Motion Event){
int action = event.getAction();
if(action==MotionEvent.ACTION_MOVE){
// ... methods that provoke that value1 changes
}else if(action==MotionEvent.ACTION_DOWN){
// ...
}else if(action==MotionEvent.ACTION_UP){
// ...
}
}
// ...
return true;
}
由于DrawFigures的onTouch方法,我无法在MainActivity的文本视图中接收值1以显示它,因为它正在更改。这个问题怎么解决?在活动中实现OnTouch方法?怎么做?MainActivity.class如下所示:
public class MainActivity extends Activity implements View.OnTouchListener{
DrawFigures fig;
TextView txtInfo;
// ...
public void btnSecRec(View v){
int id = v.getId();
if (id == R.id.btnSecRec){
RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
parent.removeAllViews();
fig = new DrawRectangleWithDimensions(this);
fig.setOnTouchListener(this);
fig.setFigure(10, 40);
parent.addView(fig);
}
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_MOVE) {
if (fig.touching == true) {
String value = Float.toString(fig.getChangingDimensionValue());
txtInfo.setText(value);
}
}
return true;
}
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
String value = Double.toString(DrawFigures.value1);
if (action == MotionEvent.ACTION_MOVE) {
if (fig.touching == true) {
String value = Float.toString(fig.getChangingDimensionValue());
txtInfoc.setText(value);
}
}
可以在活动中实现onTouchListener,在调用超类“onTouchEvent”后,通过getter更新值。大概是这样的:
@Override
public boolean onTouch(final View v, final MotionEvent event) {
super.onTouchEvent(event)
float value1 = fig.getUpdatedValues()
return true;
}
还记得在活动中为该视图分配OnTouchListener,您可以在非活动类中将value1声明为static,如下所示
静态浮点值1代码>
然后在活动类中获得如下值:
public class MainActivity extends Activity implements View.OnTouchListener{
DrawFigures fig;
TextView txtInfo;
// ...
public void btnSecRec(View v){
int id = v.getId();
if (id == R.id.btnSecRec){
RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
parent.removeAllViews();
fig = new DrawRectangleWithDimensions(this);
fig.setOnTouchListener(this);
fig.setFigure(10, 40);
parent.addView(fig);
}
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_MOVE) {
if (fig.touching == true) {
String value = Float.toString(fig.getChangingDimensionValue());
txtInfo.setText(value);
}
}
return true;
}
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
String value = Double.toString(DrawFigures.value1);
if (action == MotionEvent.ACTION_MOVE) {
if (fig.touching == true) {
String value = Float.toString(fig.getChangingDimensionValue());
txtInfoc.setText(value);
}
}
或者,由于您的值是私有的,您可以使用setter
和getter
方法来设置和检索DrawFigures对象的值,然后可以在整个应用程序中使用
如果您希望保留该值,可以使用尝试一段时间后,我找到了一个解决方案。只需使用MainActivity的onTouch方法内的实例“fig”调用DrawFigures类中的onTouchEvent方法
我要感谢那些花时间帮助我的人,谢谢
代码如下:
public class MainActivity extends Activity implements View.OnTouchListener{
DrawFigures fig;
TextView txtInfo;
// ...
public void btnSecRec(View v){
int id = v.getId();
if (id == R.id.btnSecRec){
RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
parent.removeAllViews();
fig = new DrawRectangleWithDimensions(this);
fig.setOnTouchListener(this);
fig.setFigure(10, 40);
parent.addView(fig);
}
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
fig.onTouchEvent(event);
if (action == MotionEvent.ACTION_MOVE) {
if (fig.touching == true) {
String value = Float.toString(fig.getChangingDimensionValue());
txtInfo.setText(value);
}
}
return true;
}
}为什么不将DrawFigures
作为MainActivity
的内部类,并将value1
作为全局类成员?或者,将您希望在MainActivity
中访问的值保存在SharedReference
中,DrawFigures类是一个相当大的类,它还被其他子类扩展,所以把它放在主要活动中是不方便的。关于SharedReference,在哪里使用它?主要问题是在调用DrawFigure的onTouch方法时使用value1。我需要在调用DrawFigure.class的onTouch方法时获取value1。您编写的代码应该放在Activity.class中的什么位置?我尝试在Activity类中使用onTouch方法,但未成功。请确保Drawfigures类中的value1已指定值,然后再尝试在Activity类中使用它,但仍然不起作用。现在看起来好像没有加载DrawFigures的onTouch方法中的代码。我刚刚编辑了我的问题,添加了一个在按钮被预剪切时调用的方法,它可能会产生影响。您是否初始化了按钮?按钮是用XML创建的,我没有在MainActivity中初始化它。按钮(有四个)工作正常。“fig”是DrawFigures的一个实例。此类具有用于检索值1的getter方法。但是我试过这个代码,但它不起作用。编辑的版本应该可以工作,基本上是float value1=fig.getUpdatedValues()。因此,从自定义视图“fig”中,您可以得到值1,该值会针对每个MotionEvent.ACTION\u Move进行更新,但仍然不起作用。似乎DrawFigures的onTouch方法中的代码没有加载。您正在调用super吗??super.onTouchEvent(事件)