这是Android应用程序的Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sanzuup150"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sanzuuq150"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sanzuur150"
android:layout_gravity="center_horizontal" />
</LinearLayout>
我对android开发很新,所以我不知道如何通过不同的url为imageButton2和imageButton3创建addListenerOnButton。请帮帮我......
这是MainActivity.java
package com.android.screation;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sanzuu.blogspot.com"));
startActivity(browserIntent);
}
});
}
}
答案 0 :(得分:1)
应该在每个imageButton上放置一个监听器。 每个imageButton都是xml中的一个项目,然后应该(让我们说)使用id链接到java中。因此,同一个xml中的ID应该是唯一的。在使用之前始终将元素链接到xml中的引用,否则代码将为null指针异常。
package com.android.screation;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
ImageButton imageButton2;
ImageButton imageButton3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.imageButton1); // refer to the id in the xml
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton3 = (ImageButton) findViewById(R.id.imageButton3);
//add the listener to the specific button
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "This is button 1",
Toast.LENGTH_LONG).show();
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sanzuu.blogspot.com"));
startActivity(browserIntent);
}
});
imageButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//what imageButton2 is supposed to do
Toast.makeText(getApplicationContext(), "This is button 2",Toast.LENGTH_LONG).show();
}
});
imageButton3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// what imageButton3 is supposed to do here
Toast.makeText(getApplicationContext(), "This is button 3",Toast.LENGTH_LONG).show();
}
});
}
}
如果您需要更多帮助,请随时提出。