Android 对象可在类中全局访问

发布于 2024-11-18 20:57:33 字数 662 浏览 3 评论 0原文

如何在活动中全局访问视图按钮文本视图的对象...我已经使用了下面的代码,但它现在正在工作..

 private View dummy = (View) View.inflate(this, R.layout.main, null);
 private TextView p1 = (TextView)dummy.findViewById(R.id.player1other);
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dummy);
 }
 .....
 .....
  public OnClickListener mCorkyListener = new OnClickListener() {

        @Override
    public void onClick(View v) {

        switch(v.getId())
        {
        case R.id.player1:      
        p1.setText(Integer.toString(scores.scores[0])); 

                       break;     }}

how to i make a object of views buttons textviews globally accessible in the activity... i have used the below code but its now working..

 private View dummy = (View) View.inflate(this, R.layout.main, null);
 private TextView p1 = (TextView)dummy.findViewById(R.id.player1other);
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dummy);
 }
 .....
 .....
  public OnClickListener mCorkyListener = new OnClickListener() {

        @Override
    public void onClick(View v) {

        switch(v.getId())
        {
        case R.id.player1:      
        p1.setText(Integer.toString(scores.scores[0])); 

                       break;     }}

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

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

发布评论

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

评论(5

德意的啸 2024-11-25 20:57:33

您无法在活动启动之前提供活动参考。您可以通过这种方式完成您想要的工作

private View dummy;
 private TextView p1;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dummy);
        dummy = (View) View.inflate(this, R.layout.main, null);
        p1 = (TextView)dummy.findViewById(R.id.player1other);
 }
 .....
 .....
  public OnClickListener mCorkyListener = new OnClickListener() {

        @Override
    public void onClick(View v) {

        switch(v.getId())
        {
        case R.id.player1:      
        p1.setText(Integer.toString(scores.scores[0])); 

                       break;     }}

You cannot give reference of Activity before its initiation. You can do your desired work in this way

private View dummy;
 private TextView p1;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dummy);
        dummy = (View) View.inflate(this, R.layout.main, null);
        p1 = (TextView)dummy.findViewById(R.id.player1other);
 }
 .....
 .....
  public OnClickListener mCorkyListener = new OnClickListener() {

        @Override
    public void onClick(View v) {

        switch(v.getId())
        {
        case R.id.player1:      
        p1.setText(Integer.toString(scores.scores[0])); 

                       break;     }}
人生百味 2024-11-25 20:57:33

将您的视图声明为类变量

Class A{
EditText edit1;

}

edit1 将可以在类中的任何位置访问

Declare your views as class variable

Class A{
EditText edit1;

}

edit1 will be accessible anywhere in the class

酒绊 2024-11-25 20:57:33

在使用findViewByID()之前,您需要首先调用setContentView(),以便findViewByID()知道在哪里查找视图。像这样:

private TextView p1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    p1 = (TextView)dummy.findViewById(R.id.player1other);
}

即首先声明p1,然后在onCreate()中调用适当的setContentView(),然后初始化p1 >。

You need first to call setContentView() before using findViewByID() in order findViewByID() to know where to look for the views. Like this:

private TextView p1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    p1 = (TextView)dummy.findViewById(R.id.player1other);
}

i.e. first just declare p1, then call appropriate setContentView() in onCreate(), then initialize p1.

青瓷清茶倾城歌 2024-11-25 20:57:33

而不是使用这一行....

private View dummy = (View) View.inflate(this, R.layout.main, null); 

使用这个

LayoutInflater l1 = this.getLayoutInflater();
private View dummy = l1.inflate(R.layout.main, null); 

Rather than using this line ....

private View dummy = (View) View.inflate(this, R.layout.main, null); 

Use this

LayoutInflater l1 = this.getLayoutInflater();
private View dummy = l1.inflate(R.layout.main, null); 
阿楠 2024-11-25 20:57:33

试试这个:

private TextView p1;

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

    p1 = (TextView) findViewById(R.id.player1other);
}

其余的都一样。

Try this:

private TextView p1;

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

    p1 = (TextView) findViewById(R.id.player1other);
}

and the rest is just the same.

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