java - 从普通类方法开始新 Activity

标签 java android android-activity

我不知道如何在一个类中编写一个方法来启动另一个 Activity 。

我有一个带有 5 个按钮的页脚,每个按钮都应该启动一个新 Activity 。我想用启动 Activity 的 5 种方法创建一个类。

我想做这样的事情:

我的 Footer_buttons 类:

public class Footer_buttons{

//Back to Home activity
    public static void home_footer(Context context) {   

        Intent intent = new Intent(context, Home_page.class);
        context.startActivity(intent);
    }
}

在我的一项 Activity 中,我想这样称呼:

    private static Context context;
....
        context = this;
....

    public void home_footer(View view) {    
        Footer_buttons.home_footer(context);
    }

最佳答案

您可以通过几种不同的方式指定按钮应执行的行为。

xml onClick 属性 首先,按钮有一个名为 onClick 的 xml 属性。您可以为该属性分配一个方法名称:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here"
    android:onClick="goToActivity" />

此按钮将调用此布局所属的 Activity 中的 goToActivity 方法。

public void goToActivity(View view) {
 Intent i = new Intent(this,NewActivity.class);
 startActivity(i);

fragment 中的 onClickListener 以下示例在 fragment 的 onCreateView 事件期间将 onClickListener 应用于 fragment 布局中的按钮。

这是 fragment 的 xml 中的按钮:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here" />

请注意,我们不再使用按钮的 onClick xml 属性。

onClickListener 是一个接口(interface),可以作为 fragment 类内部的匿名类实现:

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

// Find your button in the layout.
Button btnMyButton = (Button) rootView.findViewById(R.id.btnMyButton);

btnMyButton.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
        Intent i = newIntent(getActivity(),NewActivity.class);
        startActivity(i);
  }

});

Activity 中的 onClickListener 以下示例在 fragment 的 onCreate 事件期间将 onClickListener 应用于 Activity 布局中的按钮。

这是 fragment 的 xml 中的按钮:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here" />

再一次,我们不使用按钮的 onClick xml 属性。

onClickListener 接口(interface)现在作为 Activity 类内部的匿名类实现:

    // Find your button in the layout.
Button btnMyButton = (Button)findViewById(R.id.btnMyButton);

btnMyButton.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
        Intent i = newIntent(this,NewActivity.class);
        startActivity(i);
  }

});

在运行时查找 xml 元素

在运行时查找 xml 元素,如前面 2 个示例所示,需要为元素分配一个 id:

android:id="@+id/btnMyButton"

并且调用代码中引用了此 ID:

R.id.btnMyButton

当一个activity在其布局中寻找元素时,它可以直接调用findByView方法,如下所示:

Button btnMyButton = (Button)findViewById(R.id.btnMyButton);

当 fragment 在其布局中查找元素时,它必须首先在自己的 View 上调用 findViewByID,如下所示:

Button btnMyButton = (Button) rootView.findViewById(R.id.btnMyButton);

类型转换

请注意,在这两个示例中,findViewByID 的返回值都被强制转换为声明的类型 - 在本例中为 Button。

Button btnMyButton = (Button)...

findViewByID 默认返回一个 View - View 是 Button 的父级,代表最通用的类​​型。

关于java - 从普通类方法开始新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27236018/

相关文章:

java - 在 JUnit 测试中使用 JMockit 多次模拟静态方法

java - 使用 jackson ObjectMapper 时保留的堆大小更大

android - Cordova 模拟 android 退出代码 1

java - Android R18 更新后从调试 View 打开源代码编辑 .class

Android- noHistory = true 从最近回来时不工作

java - java中的舍入值

Java:如何在覆盖已弃用成员的派生接口(interface)中避免弃用警告?

android - getLine1Number() 返回空白,不为空

android - 如何在应用程序关闭/最小化时停止 MediaPlayer 声音?

android - 我是否正确地在 Activity 代码中进行依赖注入(inject)?