添加链接到另一个活动

时间:2013-08-26 11:26:33

标签: java android

我想使用以下代码添加按钮以链接到RegisterActivity上的另一个活动

    public void setOnAccountCreationFirstViewListener(OnAccountCreationFirstViewListener listener) {
        mListener = listener;
    }

    /**
     * Interface for listeners of {@link AccountCreationFirstView} 
     * see {@link AccountCreationFirstView#setOnAccountCreationFirstViewListener}
     */
    public interface OnAccountCreationFirstViewListener {
        /**
         * User asked to create the account
         */
        /**
         * User asked to edit : he has an existing account
         */
        void onEditAccountRequested();
    }
}

public void setOnAccountCreationFirstViewListener(OnAccountCreationFirstViewListener listener) { mListener = listener; } /** * Interface for listeners of {@link AccountCreationFirstView} * see {@link AccountCreationFirstView#setOnAccountCreationFirstViewListener} */ public interface OnAccountCreationFirstViewListener { /** * User asked to create the account */ /** * User asked to edit : he has an existing account */ void onEditAccountRequested(); } } 任何人都可以帮我添加一个名为RegisterActivity的活动按钮 按钮ID是button2

3 个答案:

答案 0 :(得分:1)

更改按钮的xml并添加android:onClick="openActivity" e.g。

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="openActivity"
    android:text="@string/button" />

然后将此函数添加到其类布局包含button1

的Activity类中
public void openActivity(View view) 
{
    Intent intent = new Intent(this, RegisterActivity .class);
    startActivity(intent);
}

答案 1 :(得分:0)

您可以将OnClickListener设置为要添加操作的按钮。然后开始去另一个活动的意图。

button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(getApplicationContext(),
                            RegisterActivity.class);
                    startActivity(i);
                }
            });

答案 2 :(得分:0)

尝试理解代码,然后像这样修改代码 在这里,我从SecondActivity开始FirstActivity FirstActivity.java

Button button2;
onCreate(...){
   super(...);
   setContentView(...);
   button2=(Button)findViewById(R.id.button2);
   button2.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent i = new Intent(getApplicationContext,
                          SecondActivity.class);
                  startActivity(i);
              }
          });
}

AndroidManifest.xml

<application ....>
 <activity name=".FisrtActivity">
<intent-filter>
    ...
</intent-filter>
 </activity>
<activity name=".SecondActivity"/>
</application>