如何让我的ImageButton转到Android Studio中的另一个活动?

时间:2015-08-07 03:42:26

标签: android android-intent android-imagebutton

我想知道如何让我的ImageButton转到 Android Studio 1.2.2 中的其他活动?

我尝试使用按钮的方式制作它。

这是我的Java代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_01);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_menu_01, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);

  }
}

2 个答案:

答案 0 :(得分:0)

以下是您可以使用的示例代码。我们的想法是为您的OnClickListener设置ImageButton,并在OnClickListener中创建一个Intent以转到您的新Activity,然后致电{{1} }}

startActivity(intent)

答案 1 :(得分:0)

首先需要在当前活动的layout.xml上创建一个ImageButton:

<ImageButton
    android:id="@+id/your_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

在当前活动中,您需要创建一个变量ImageButton:

ImageButton myButton = (ImageButton) findViewById(R.id.the_button_you_created_on_the_layout.xml_file);

然后,您需要为此按钮设置单击侦听器:

*点击监听器将等待&#34;点击按钮。

myButton.setOnClickListener(new View.OnClickListner(){
    // When the button is pressed/clicked, it will run the code below
    @Override
    public void onClick(){
        // Intent is what you use to start another activity
        Intent intent = new Intent(this, YourActivity.class);
        startActivity(intent);
    }
});

之后,您的应用应该可以毫无问题地启动其他活动。