Android,从代码调用视图对象

发布于 2024-12-12 01:14:23 字数 549 浏览 0 评论 0原文

我是一个新手,所以如果这个问题太简单或太难,请原谅。

我的 java 文件中有这段代码:

public void button_baby_clicked(View v) 
{
//do something here
}

当有人单击我的 xml 文件中的图像按钮时,会调用此代码,但如何从 java 文件本身调用此代码? 因为它需要一个 View 对象......我猜我需要重新创建它?如何?

编辑:
好的,澄清一下,我希望能够通过单击 xml 文件及其下的函数来调用上述函数。 例如:

    public void button_baby_clicked(View v) 
    {
    //do something here
    }

 public void someFunction() 
    {
    x = 10;
    button_baby_clicked(); // This should call the above function.
    }

I am kind of a newbie so excuse me if this question is too simple or too hard.

I have this code in my java file:

public void button_baby_clicked(View v) 
{
//do something here
}

this gets called when someone clicks the imagebutton in my xml file, but how do I call this from the java file itself?
Because it's expecting a View object... and I'm guessing I need to recreate that? How?

Edit:
Ok, to clarify, I want to be able to call the above function via a click in my xml file as well as a function under it.
For example:

    public void button_baby_clicked(View v) 
    {
    //do something here
    }

 public void someFunction() 
    {
    x = 10;
    button_baby_clicked(); // This should call the above function.
    }

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

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

发布评论

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

评论(4

梨涡少年 2024-12-19 01:14:23

在您的 ImageButton 中,您必须添加一个属性: android:onClick="button_baby_clicked"
在 java 文件中,您添加了:

public void button_baby_clicked(View v) 
{
//do something here
}

其背后的逻辑是:
单击您的图像按钮后,将自动调用此方法,即“v”参数将具有您的图像按钮。

这样做的好处是:您不需要在您的活动中初始化图像按钮,也不需要为此图像按钮设置点击侦听器。

In ur ImageButton you have to add an attribute: android:onClick="button_baby_clicked"
In the java file, you have added:

public void button_baby_clicked(View v) 
{
//do something here
}

The logic behind this is:
Upon clicking ur imagebutton, this method will automatically get called, i.e "v" argument will be having ur imagebutton.

The advantage of giving like this is: You no need to initialize the imagebutton in ur activity and no need to set click listener too for this imagebutton.

情定在深秋 2024-12-19 01:14:23

好吧,如果您希望每次单击视图时都调用该方法,请按照其他人所说的操作。

或者,您可以执行类似的操作。

ImageView globalReference;

@Override
public void onCreate(Bundle icicle){
   *** CODE ***
   globalReference = (ImageView) findViewById(R.id.myImageView);
   *** CODE ***
}

然后,每当您希望使用特定的 View 调用它时,只需调用

button_baby_clicked(globalReference);

您也可以使用任何 View 执行此操作您动态创建的对象。

View myTv = new TextView(context);
View myLl = new LinearLayout(context);

button_baby_clicked(myTv);
button_baby_clicked(myLl);

只需获取与该方法相同作用域内的有效 View 引用,然后像任何其他方法一样将其传入。如果该方法能够处理它,它甚至可以为 null。

Alright, if you want to have the method invoked every time the view is clicked, do what the others have said.

Alternatively, you can do something like this.

ImageView globalReference;

@Override
public void onCreate(Bundle icicle){
   *** CODE ***
   globalReference = (ImageView) findViewById(R.id.myImageView);
   *** CODE ***
}

Then, whenever you want that to be called with that particular View, simply call

button_baby_clicked(globalReference);

You can also do this with any View object you create dynamically.

View myTv = new TextView(context);
View myLl = new LinearLayout(context);

button_baby_clicked(myTv);
button_baby_clicked(myLl);

Just get a valid View reference within the same scope as the method, and pass it in like any other method. It can even be null if the method is capable of handling it.

独行侠 2024-12-19 01:14:23

你不能像- 那样使用它吗

mButton.setOnClickListener(new OnClickListener{

       @Override
       public void onClick(View v) {
                
           button_baby_clicked(v);
       }      
  }
);  

编辑:

如果您需要从按钮的onClick调用someFunction(),并从那里调用button_baby_clicked(),您必须在someFunction中获取View v对象。此链接可能对您有帮助。请参阅在onClick上启动服务。你可以适当改变。

Can't you use it like -

mButton.setOnClickListener(new OnClickListener{

       @Override
       public void onClick(View v) {
                
           button_baby_clicked(v);
       }      
  }
);  

??

EDIT :

If you need to call someFunction() from the onClick of a button,and from there,you need to call button_baby_clicked(),you have to get View v object in someFunction. This link might help you. Please refer Start a service on onClick. You can change appropriately.

魔法少女 2024-12-19 01:14:23

我相信最好重构代码并将事件处理程序中的代码放入可以从任何地方调用的全局方法中。像这样:

public void button_baby_clicked(View v) 
{
  taskToPerform(); // Perform a certain task
}

public void someFunction() 
{
  x = 10;
  taskToPerform(), // Perform the same task again
}

public void taskToPerform()
{
   //This is where you write the task you want to perform
}

这样您就可以随时随地重用 taskToPerform() 方法中的代码。

I believe its best if you refactor your code and put the code in the event handler into a global method that can be called from anywhere. like this:

public void button_baby_clicked(View v) 
{
  taskToPerform(); // Perform a certain task
}

public void someFunction() 
{
  x = 10;
  taskToPerform(), // Perform the same task again
}

public void taskToPerform()
{
   //This is where you write the task you want to perform
}

This way you can reuse the code in the taskToPerform() method anywhere, anytime.

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