简体   繁体   English

从普通的类方法开始新活动

[英]Start new activity from a normal class method

I don't know how to write a method in a class that would start another activity. 我不知道如何在一个可以启动另一个活动的类中编写方法。

I have a footer with 5 buttons, and every button should start a new activity. 我有一个带5个按钮的页脚,每个按钮都应该开始一个新的活动。 And I would like to create a class with the 5 methods that are starting the activities. 我想用5个开始活动的方法创建一个类。

I would like to do something like that: 我想做那样的事情:

My Footer_buttons class: 我的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);
    }
}

In one of my activities I would like to call something like that: 在我的一项活动中,我想称之为:

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

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

You can specify the behaviour a button should perform in a couple of different ways. 您可以通过几种不同的方式指定按钮应执行的行为。

xml onClick attribute Firstly, buttons have an xml attribute called onClick. xml onClick属性首先,按钮有一个名为onClick的xml属性。 You can assign a method name to this attribute: 您可以为此属性指定方法名称:

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

This button will call the goToActivity method in the Activity that this layout belongs to. 此按钮将调用此布局所属的Activity中的goToActivity方法。

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

} }

onClickListener in a fragment The following example applies an onClickListener to a button in a fragment's layout during the fragment's onCreateView event. 片段中onClickListener以下示例在片段的onCreateView事件期间将onClickListener应用于片段布局中的按钮。

Here is the button in the fragment's xml: 这是片段的xml中的按钮:

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

Note that we no longer use the onClick xml attribute of the button. 请注意,我们不再使用按钮的onClick xml属性。

The onClickListener is an interface and can be implemented as an anonymous class inside of the fragment class: onClickListener是一个接口,可以在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);
  }

});

onClickListener in an Activity The following example applies an onClickListener to a button in an Activity's layout during the fragment's onCreate event. 活动中onClickListener以下示例在片段的onCreate事件期间将onClickListener应用于Activity的布局中的按钮。

Here is the button in the fragment's xml: 这是片段的xml中的按钮:

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

Once again, we do not use the onClick xml attribute of the button. 我们再次使用按钮的onClick xml属性。

The onClickListener interface is now implemented as an anonymous class inside of the activity class: onClickListener接口现在作为活动类内部的匿名类实现:

    // 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);
  }

});

Finding xml elements at runtime 在运行时查找xml元素

Finding xml elements at runtime, as shown in the previous 2 examples requires that the elements are assigned an id: 在运行时查找xml元素,如前两个示例所示,需要为元素分配一个id:

android:id="@+id/btnMyButton"

and that this ID is referenced in the calling code: 并且在调用代码中引用了此ID:

R.id.btnMyButton

When an activity is looking for elements in its layout, it can call the findByView method directly, as in below: 当一个活动在其布局中查找元素时,它可以直接调用findByView方法,如下所示:

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

When a fragment is looking for elements in its layout, it must call findViewByID on its own view first, as in below: 当片段在其布局中查找元素时,它必须首先在其自己的视图上调用findViewByID,如下所示:

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

Casting 铸件

Note that in both examples, the return value of findViewByID is being cast to the declared type - in this case Button. 请注意,在两个示例中,findViewByID的返回值都被强制转换为声明的类型 - 在本例中为Button。

Button btnMyButton = (Button)...

findViewByID returns a View by default - View is a parent of Button and represents the most general type. findViewByID默认返回View - View是Button的父级,代表最常规的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM