Java 如何在 Android Studio 上使用 View Parameter 调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27737062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:56:24  来源:igfitidea点击:

How to call method with View Parameter on Android Studio

javaandroidmethodsviewparameters

提问by user2565280

I want to call this method

我想调用这个方法

public void openButton(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    this.startActivity(intent);
}

from simple method like this

从这样的简单方法

public void simple(){
    openButton();
}

but I can't do it because openButton needs one parameter View. How?

但我不能这样做,因为 openButton 需要一个参数 View。如何?

采纳答案by Alex K

Well, with the code that you provided, you usually use some sort of an onCickListener.

好吧,对于您提供的代码,您通常会使用某种onCickListener.

Open your XML file, and add android:onClick="openButton"to the button that you want to call that method. So your XML for the button will look something like this:

打开您的 XML 文件,并添加android:onClick="openButton"到您要调用该方法的按钮。因此,按钮的 XML 将如下所示:

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Click me!"
   . . . 
   android:onClick="openButton" />

That will automatically call that method and pass in a view.

这将自动调用该方法并传入一个视图。

The other option, as BatScream mentioned in the comments, is to just pass in null, as you aren't using the view anyway. HOWEVER, this is bad practice - it'll work this time, but in general, you should follow the system that Android uses. Just go with an onClickin the XML.

另一种选择,正如 BatScream 在评论中提到的,只是传入null,因为无论如何你都没有使用视图。然而,这是不好的做法 - 这次它会起作用,但总的来说,您应该遵循 Android 使用的系统。只需onClick在 XML 中使用 。



If you HAVE to use simplethe way it is, do it this way:

如果您必须使用simple它的方式,请这样做:

public void simple(){
    openButton(null);
}

回答by codeMagic

You should be able to do

你应该能够做到

 button.performClick(); 

assuming openButton()is the method assigned to buttons onClick. Meaning, somewhere in your xml you probably have a Buttonwith android:onClick="openButton". Then if you have that Buttoninstantiated and assigned to the variable button, calling the View's performClick()method would call openButton()

假设openButton()是分配给buttons的方法onClick。意思是,在您的 xml 中的某个地方,您可能有一个Buttonwith android:onClick="openButton". 然后,如果您已将其Button实例化并分配给变量button,则调用ViewperformClick()方法将调用openButton()

回答by Chintan Soni

Simple. Just pass the view in arguments.

简单的。只需在参数中传递视图。

Way 1:if you are calling openButton()method from layout file then, simply call method the following way by applying onClick attribute

方式 1:如果您openButton()从布局文件调用方法,只需通过应用 onClick 属性按以下方式调用方法

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Go to next screen"
   . . . 
   android:onClick="openButton" />

Way 2:if you are trying to call it from button's onclick by setting OnClickListener, then, simply pass the view you are getting inside OnClickListener's onClick(View view)method as follows:

方式 2:如果您尝试通过设置 OnClickListener 从按钮的 onclick 调用它,则只需按如下方式传递您正在进入的视图OnClickListeneronClick(View view)方法:

button.setOnClickListener(new OnClickListener(){

    @override
    public void onClick(View view)
    {
       openButton(view);
    }

});