Android ImageButton onClick

Android ImageButton onClick,android,Android,我对activity_main.xml中的按钮有问题 我需要通过点击“imageButton8”启动应用程序中的webView(网站URL) MainActivity.Java imageButton8的activity_main.xml为 activity_webview.xml 在同一个项目中, 2.其他问题:我有08个按钮。如何在其模板xml中执行每个按钮 例如:按钮1=>about_obama.xml。当用户单击(在此文件中)时,将找到obama的描述。 按钮2=>…xml…等等!

我对activity_main.xml中的按钮有问题

  • 我需要通过点击“imageButton8”启动应用程序中的webView(网站URL)
  • MainActivity.Java

    imageButton8的activity_main.xml为

    activity_webview.xml

    
    

    在同一个项目中, 2.其他问题:我有08个按钮。如何在其模板xml中执行每个按钮

    例如:按钮1=>about_obama.xml。当用户单击(在此文件中)时,将找到obama的描述。 按钮2=>…xml…等等!
    按钮3……

    您需要使用ImageButton的setOnClickListener()方法为ImageButton设置onclickListener。因此,在MainActivity的onCreate()方法中,执行以下操作:

    Button imageButton8 = (Button) findViewById(R.id.imageButton8);
    // Set the OnClickListener on the button itself
    imageButton8.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Activity2.class); // Use the MainActivity context
            startActivityForResult(intent, 0);
        }
    });
    // You should delete the private setOnClickListener() method as it does not
    // have any purpose
    
    要在单击按钮时在xml中指定onClick(),请设置:

    android:onClick:"some_method"
    
    属性为您的按钮。然后在活动中声明并定义此方法

    package com.XX.app;
    
    import com.XX.app.R;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.app.Application;
    import android.os.Bundle;
    
    public class Activity2 extends Activity {
        @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_webview);
        }
    }
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_1">
    
        <WebView
            android:id="@+id/webView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
    </RelativeLayout>
    
    Button imageButton8 = (Button) findViewById(R.id.imageButton8);
    // Set the OnClickListener on the button itself
    imageButton8.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Activity2.class); // Use the MainActivity context
            startActivityForResult(intent, 0);
        }
    });
    // You should delete the private setOnClickListener() method as it does not
    // have any purpose
    
    android:onClick:"some_method"