Java 如何在android中访问子类中的主要活动内容

Java 如何在android中访问子类中的主要活动内容,java,android,xml,Java,Android,Xml,我有关于主要活动的编辑文本,我需要访问我的子类上的编辑文本 这是我的主要活动 private EditText et1; private EditText et2, et; // int dec = et.getText().toString().length()-1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC

我有关于主要活动的编辑文本,我需要访问我的子类上的编辑文本

这是我的主要活动

private EditText et1;
    private EditText et2, et;

// int dec = et.getText().toString().length()-1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);
    et = et1;
    gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);

    // glip1 = GestureLibraries.fromRawResource(this, R.raw.gestures1);

    if (!gLib.load()) {
        Log.w(TAG, "could not load gesture library");
        finish();
    }
    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    common myHandler = new common();
    gestures.addOnGesturePerformedListener(myHandler);
}
这是我的课

public class common implements OnGesturePerformedListener {


    @Override
    public void onGesturePerformed(GestureOverlayView gestureView,
            Gesture gesture) {
        System.out.println("guster");

        ArrayList<Prediction> predictions = gLib.recognize(gesture);//i have to acces glip from main activity
        // ArrayList<Prediction> predictions1 = glip1.recognize(gesture);
        // one prediction needed
        if (predictions.size() > 0 && predictions.get(0).score > 2.0) {
            String prediction = predictions.get(0).name;
            // checking prediction

            if (prediction.equals("A")) {
                // and action
                et.append("A");// i have to access edit text from main activity
                // et.getText().insert(et.getSelectionStart(), "A");
            }
        }
    }
}
公共类公共实现OnTesturePerformedListener{
@凌驾
已执行的测试的公共无效(GestureOverlayView gestureView,
(手势){
系统输出打印项次(“古斯特”);
ArrayList predictions=gLib.recognize(手势);//我必须从主活动访问glip
//ArrayList predictions1=glip1.识别(手势);
//需要一个预测
if(predictions.size()>0&&predictions.get(0).score>2.0){
字符串预测=预测.get(0).name;
//检验预测
if(预测等于(“A”)){
//和行动
et.append(“A”);//我必须从主活动访问编辑文本
//et.getText().insert(et.getSelectionStart(),“A”);
}
}
}
}

在构造函数中传递对活动的引用,并添加相关方法以在类之间通信(不要直接访问活动的字段…)

在活动中:

 common myHandler = new common(this);
通用(使用大写字母C表示名称约定):


在构造函数中传递对活动的引用,并添加相关方法以在类之间通信(不要直接访问活动的字段…)

在活动中:

 common myHandler = new common(this);
通用(使用大写字母C表示名称约定):


最简单的方法是在初始化时(即在构造函数中)将主类/对象引用传递给子类。并将引用存储在成员变量中,以便以后使用

最简单的方法是在初始化时(即在构造函数中)将主类/对象引用传递到子类中。并将引用存储在成员变量中,以便以后使用

忘记尝试从活动外部访问成员时遇到的困难

只需让您的活动实现
GesturePerformedListener

public class MyActivity implements OnGesturePerformedListener
{
  //your activity code here
}
然后,在测试侦听器时,不要创建一个
common
类,只需使用
this

gestures.addOnGesturePerformedListener(this);

这消除了对单独类的需要,并允许您直接访问TextView

忘记尝试从活动外部访问成员的困难

只需让您的活动实现
GesturePerformedListener

public class MyActivity implements OnGesturePerformedListener
{
  //your activity code here
}
然后,在测试侦听器时,不要创建一个
common
类,只需使用
this

gestures.addOnGesturePerformedListener(this);
这样就不需要单独的类,并允许您直接访问TextView