【问题标题】:how to use onclick methods on android如何在android上使用onclick方法
【发布时间】:2012-03-22 02:31:13
【问题描述】:

所以我有这五个按钮,我希望它们始终出现在所有活动中,类似于其他活动的快捷按钮。所以我为此创建了一个单独的 xml 布局,并将它们包含在其他活动布局中。我还创建了一个具有相应方法的类来处理按钮点击。

现在我的问题是我不知道如何在我的活动中使用/声明这个类。当我尝试运行我的应用程序时,logcat 给我一个错误,它找不到处理点击的方法。

我该怎么做?

这是我的按钮处理程序类:

package com.meralco.pms;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

public class ButtonsHandler extends Activity{

protected void startSin() {
    Intent launch = new Intent(this, SinActivity.class);
    startActivity(launch);
}
protected void startCity() {
    Intent launch = new Intent(this, CityActivity.class);
    startActivity(launch);
}

protected void startHelp() {
    Intent launch = new Intent(this, HelpActivity.class);
    startActivity(launch);

}
protected void startAll() {
    Intent launch = new Intent(this, AllActivity.class);
    startActivity(launch);

}
protected void startDate() {
    Intent launch = new Intent(this, DateActivity.class);
    startActivity(launch);

}
public void buttonClick(View v) {
    switch(v.getId())
      {
      case R.id.button_sin:
          Toast.makeText(v.getContext(), "SIN" , Toast.LENGTH_SHORT).show();
          startSin();
          break;
      case R.id.button_city:
          startCity();
          Toast.makeText(v.getContext(), "CITY" , Toast.LENGTH_SHORT).show();
          break;
      case R.id.button_date:
          startDate();
          Toast.makeText(v.getContext(), "DATE" , Toast.LENGTH_SHORT).show();
          break;
      case R.id.button_all:
          startAll();
          Toast.makeText(v.getContext(), "ALL" , Toast.LENGTH_SHORT).show();
          break;
      case R.id.button_help:
          startHelp();
          Toast.makeText(v.getContext(), "HELP" , Toast.LENGTH_SHORT).show();
          break;
      }
}
}

我想我缺少构造函数。我是走对了路还是完全错了?蒂亚!

【问题讨论】:

    标签: android class button


    【解决方案1】:

    我会将您的 ButtonsHandler 更改为扩展 Fragment 而不是 Activity。然后,您可以将此片段包含在您的所有其他活动中。他们必须扩展 FragmentActivity 才能托管您的 ButtonsHandler 片段,并且布局文件将引用 com.meralco.pms.ButtonsHandler 作为片段。

    为了在 3.0 之前的 Android 版本中支持 Fragment,您需要使用 Android 兼容性库:http://developer.android.com/sdk/compatibility-library.html

    您还想了解 Fragments:http://developer.android.com/guide/topics/fundamentals/fragments.html

    它们是 Android 应用程序重用 UI 组件的推荐方式,例如在这种情况下:

    Fragment 表示 Activity 中的行为或用户界面的一部分。您可以在单个活动中组合多个片段以构建多窗格 UI 并在多个活动中重用一个片段。您可以将片段视为活动的模块化部分,它有自己的生命周期,接收自己的输入事件,并且可以在活动运行时添加或删除(有点像“子活动”,您可以在不同的活动中重复使用)。

    关于您的按钮,您需要像这样连接监听器:

    Button helpButton = (Button) findViewById(R.id.button_help);
    helpButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startHelp();
            Toast.makeText(v.getContext(), "HELP" , Toast.LENGTH_SHORT).show();                   
        }
    });
    

    【讨论】:

    • 我会把听众放在哪里?在ButtonsHandler 类中?我将阅读有关片段的内容。谢谢@louielouie :)
    • 不客气。 :) 是的,如果你让 ButtonsHandler 扩展 Fragment,那么你会在 onCreateView() 中扩展 XML 布局,然后在 onActivityCreated() 中连接监听器。
    • 好的......嗯......会试试看!虽然没有在onActivityCreated() 上使用过。再次感谢!
    【解决方案2】:

    您将需要添加 OnClickListeners 以在单击按钮时进行注册。

    如果您向下滚动一点,您可以在 Android Dev Guide 上了解如何使用它们。

    【讨论】:

      【解决方案3】:

      在每个按钮的 xml 中,确保你有属性 android:onClick="buttonClick"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多