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

How Can I Make my ImageButton go to another activity in Android Studio?

我想知道如何让我的 ImageButton 转到 Android Studio 1.2.2?

中的另一个 activity

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

这是我的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);

  }
}

这里有一些示例代码供您使用。这个想法是为你的 ImageButton 设置一个 OnClickListener,然后 OnClickListener 创建一个 Intent 去你的新 Activity,然后调用 startActivity(intent).

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_01);
    ImageButton button = (ImageButton) findViewById(R.id.my_button);
    final Context context = this;
    button.setOnClickListener(new View.OnClickListner(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(context, NewActivity.class);
            startActivity(intent);
        }
    });
}

您首先需要在当前 activity:

的 layout.xml 上创建一个 ImageButton
<ImageButton
    android:id="@+id/your_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

然后,在您当前的 activity 中,您需要创建一个变量 ImageButton:

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

然后,你需要给这个按钮设置一个点击监听器:

*点击侦听器将为您的按钮中的点击 "wait"。

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

之后,您的应用应该可以正常启动另一个 activity。