在不扩展 Android 中 Activity 的类中使用 findviewbyid

发布于 2024-12-18 20:54:23 字数 160 浏览 1 评论 0原文

我有一个当前正在扩展 Activity 的类,并且有 findViewById、ArrayAdapter 等方法。 我想把它变成一个独立的类,但以上所有方法都变得未定义。问题是什么?导入类还不够吗?例如,我为 findViewById 导入 android.view.View 但它仍然没有区别。 请指教。

I have a class that is currently extending Activity and I have methods like findViewById, ArrayAdapter etc.
I want to turn it into an independent class but all the above methods become undefined. What is the problem? Shouldn't importing the classes be enough? For eg, I import android.view.View for findViewById but it still makes no difference.
Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栩栩如生 2024-12-25 20:54:23

您应该将 Activity 的实例传递给构造函数上的第二个类,如下所示:

在您的 Activity 中,像这样实例化您的类:

MyClass instance = new MyClass(this);

在您的第二个类中,构造函数 将像这样:

public class MyClass {

public Activity activity; 
//.... other attributes 

public MyClass( Activity _activity){

   this.activity = _activity;
//other initializations...

}
}

然后当你想使用 findViewById() 方法时,你可以这样做:

EditText txt = (EditText)this.activity.findViewById(R.id.txt);

you should pass the instance of your Activity to your Second Class on the constructor like this :

In your Activity Instanciate your Class like this :

MyClass instance = new MyClass(this);

And in your second Class , the constructor will be like this :

public class MyClass {

public Activity activity; 
//.... other attributes 

public MyClass( Activity _activity){

   this.activity = _activity;
//other initializations...

}
}

and then when you want to use the findViewById() method , you can do like this :

EditText txt = (EditText)this.activity.findViewById(R.id.txt);
孤星 2024-12-25 20:54:23

如果您想调用属于 Activity 的任何函数,那么您唯一需要的是 Activity 的上下文。

例如。

class A extends Activity {
    Context ctx;

    void onCreate(Bundle b)
        ctx = this;
        B bob = new B(ctx);
    }
}

这里是B类。

class B {

    B (Activity a) {
        a.anyFunctionOfActivity();
    }
}

if you want to call any function that belongs to Activity then only thing you need to have is context of the Activity.

eg.

class A extends Activity {
    Context ctx;

    void onCreate(Bundle b)
        ctx = this;
        B bob = new B(ctx);
    }
}

Here is class B.

class B {

    B (Activity a) {
        a.anyFunctionOfActivity();
    }
}
贵在坚持 2024-12-25 20:54:23

findViewByIdActivity< 的非静态公共方法/code> 类,因此它只能用于 Activity 对象。因此,导入 android.view.View 和 android.app.Activity 不会使其可用。为了使其可用,您可以传递一个 Activity 对象 - 通常是活动中的一个 this 点。但是,请注意,您应该始终在 UI 线程中更新视图。

findViewById is non-static public method of the Activity Class ,so it only be available for a Activity object. Therefore, import android.view.View and android.app.Activity won't make it available. To make it available, you could pass around a Actvity object - usually a this point in your activity. However, notice that you should always update your View in the UI thread.

爱你不解释 2024-12-25 20:54:23

请尝试以下操作:

public static View getLayoutByRes(@LayoutRes int layoutRes,@Nullable ViewGroup viewGroup)
{
    final LayoutInflater factory = getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    return factory.inflate(layoutRes, viewGroup);
}

TextView tv = (TextView) getLayoutByRes(R.layout.xxx ,Null).findViewById(R.id.editd);

Please try the following:

public static View getLayoutByRes(@LayoutRes int layoutRes,@Nullable ViewGroup viewGroup)
{
    final LayoutInflater factory = getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    return factory.inflate(layoutRes, viewGroup);
}

TextView tv = (TextView) getLayoutByRes(R.layout.xxx ,Null).findViewById(R.id.editd);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文