在android上实现OnClickListener时遇到问题

发布于 2024-12-04 05:41:02 字数 800 浏览 6 评论 0原文

我想为我的主视图上的按钮实现一个单击侦听器。我的代码如下所示

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.btnFinish);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mFinishListener);
    ...
}

private OnClickListener mFinishListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

,但显示错误如下,

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)  MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 37 Java Problem

我不知道该怎么做。请帮忙。

I want to implement a click listener for a button on my main view. My code is something like below

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.btnFinish);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mFinishListener);
    ...
}

private OnClickListener mFinishListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

But shows me error as follows

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)  MobileTrackerActivity.java  /MobileTracker/src/com/example/mobiletracker    line 37 Java Problem

I have no idea what to do. Please help.

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

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

发布评论

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

评论(6

风蛊 2024-12-11 05:41:02

您没有使用正确的接口来实例化 mFinishLinstener 变量...

您可能有一个指定 DialogInterface 的导入,这会混淆视图。

尝试显式指定View.OnClickListener

private View.OnClickListener mFinishListener = new View.OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

You are not using the correct interface to instantiate the mFinishLinstener variable...

It is possible you have an import specifying DialogInterface and that is confusing the view.

Try specifying View.OnClickListener explicitly.

private View.OnClickListener mFinishListener = new View.OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};
忆沫 2024-12-11 05:41:02

根据我的意见,实现按钮单击事件的最佳方法。

您可以使用 android:onClick 属性为 XML 布局中的按钮分配一个方法,而不是将 OnClickListener 应用于活动中的按钮。例如:

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

现在,当用户单击按钮时,Android 系统会调用 Activity 的 selfDestruct(View) 方法。为了使其工作,该方法必须是公共的并接受视图作为其唯一参数。例如:

public void selfDestruct(View view) {
     // Kabloey
 }

注意:上面的代码在 Android SDK - Button 中给出。

As per my opinion Best way to implement On click event for the Button.

Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:

public void selfDestruct(View view) {
     // Kabloey
 }

Note: The above code is given in Android SDK - Button.

涙—继续流 2024-12-11 05:41:02

试试这个代码::

final Button button = (Button) findViewById(R.id.btnFinish);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });

try this code :::

final Button button = (Button) findViewById(R.id.btnFinish);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
回忆躺在深渊里 2024-12-11 05:41:02

只需尝试一下这个:

 button.setOnClickListener(new OnClickListener() {  
@Override   
 public void onClick(View v) {
      // do something when the button is clicked
    }
};

Simply try this one as:

 button.setOnClickListener(new OnClickListener() {  
@Override   
 public void onClick(View v) {
      // do something when the button is clicked
    }
};
零度℉ 2024-12-11 05:41:02

你也可以使用像下面的代码..

Button button = (Button)findViewById(R.id.btnFinish);

button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
   //Write Your code here
}
});

you can also use like below code..

Button button = (Button)findViewById(R.id.btnFinish);

button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
   //Write Your code here
}
});
爺獨霸怡葒院 2024-12-11 05:41:02

您还可以在 xml 中声明 onclick。

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="buttonClick" />

在您的代码中,您可以将函数定义为:

public void buttonClick(View view)
{
    // handle click
}

You can also declare the onclick in the xml.

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="buttonClick" />

And in your code you would define the function as:

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