tôi muốn áp dụng sự kiện onClickListener cho một imageView, làm cách nào để thực hiện điều này? bất cứ ai có thể cung cấp cho tôi với một số ví dụ mã nguồncách áp dụng trình xử lý sự kiện nhấp chuột vào hình ảnh trong android
29
A
Trả lời
75
ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
}
});
24
Trong xml:
<ImageView
android:clickable="true"
android:onClick="imageClick"
android:src="@drawable/myImage">
</ImageView>
Trong mã
public class Test extends Activity {
........
........
public void imageClick(View view) {
//Implement image click function
}
1
Hãy thử ví dụ này.
activity_main.xml:
<LinearLayout 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"
tools:context=".MainActivity" >
<GridView
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/grid"
android:background="#fff7ff"
/>
</LinearLayout>
grid_single.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="60dp"
android:layout_height="60dp"
>
</ImageView>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textSize="9sp"
android:textColor="#3a0fff">
</TextView>
</LinearLayout>
CustomGrid.java:
package com.example.lalit.gridtest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] web;
private final int[] Imageid;
public CustomGrid(Context c, String[] web, int[] Imageid) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
MainActivity.java:
package com.example.lalit.gridtest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView grid;
String[] web = {
"Mom",
"Mahendra",
"Narayan",
"Bhai",
"Deepak",
"Sanjay",
"Navdeep",
"Lovesh",
};
int[] imageId = {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id){
if (web[position].toString().equals("Mom")) {
try {
String uri ="te:"+ "9009388988";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
if (web[position].toString().equals("Mahendra")) {
try {
String uri = "tel:" + "99669660948";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
if(web[position].toString().equals("Narayan")){
try {
String uri = "tel:" + "8889115906";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
if(web[position].toString().equals("Bhai")){
try {
String uri = "tel:" + "9893352005";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
if(web[position].toString().equals("Deepak")){
try {
String uri = "tel:" + "7869302868";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
if(web[position].toString().equals("Sanjay")){
try {
String uri = "tel:" + "9584124849";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
if(web[position].toString().equals("Navdeep")){
try {
String uri = "tel:" + "8602475687";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
if(web[position].toString().equals("Lovesh")){
try {
String uri = "tel:" + "8871512449";
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Your call has failed...",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lalit.gridtest" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Các vấn đề liên quan
- 1. Thêm trình xử lý sự kiện nhấp chuột vào Pushpin
- 2. Xử lý DataGridHyperlinkColumn Nhấp vào Sự kiện
- 3. WP7 - Sự kiện nhấp chuột hình ảnh?
- 4. Cách thêm trình xử lý nhấp chuột động vào jeditable
- 5. Xử lý mục menu Nhấp vào Sự kiện - Android
- 6. Thêm trình xử lý sự kiện nhấp vào iframe
- 7. Xử lý tất cả các sự kiện chuột trong Android
- 8. Làm cách nào để sử dụng event.preventDefault với trình xử lý sự kiện nhấp KnockoutJs?
- 9. Thêm trình xử lý nhấp chuột vào LI bullet
- 10. Xử lý các sự kiện nhấp chuột trên các lớp z-index'd
- 11. Làm cách nào để xử lý các sự kiện nhấp chuột phải trong angular.js?
- 12. Xử lý nút chuột phải Nhấp đúp chuột vào hình dạng
- 13. html5, thêm một trình xử lý sự kiện vào một hình ảnh được vẽ trên canvas
- 14. Android xử lý sự kiện GUI như thế nào?
- 15. Xử lý nhấp chuột vào nút "Đăng ký" của Facebook
- 16. Cách xử lý chính xác các sự kiện nhấp chuột trên Widget
- 17. cách nhấp hoặc nhấp đúp vào một từ trên trang web để kích hoạt trình xử lý sự kiện?
- 18. Làm cách nào để xử lý sự kiện nhấp trong Listview trong Android?
- 19. Javascript ảnh Object - Xử lý sự kiện onload
- 20. jQuery nhiều trình xử lý sự kiện - Cách hủy?
- 21. Sự kiện mờ dừng sự kiện nhấp chuột hoạt động?
- 22. Monodroid - Xử lý Nhấp vào các sự kiện bên trong hàng ListAdapter
- 23. WPF: Cách gán sự kiện nhấp chuột vào biểu mẫu
- 24. cách kích hoạt sự kiện khi ai đó nhấp vào bất kỳ đâu trên màn hình trong ứng dụng android?
- 25. Chuyển dữ liệu bằng cách sử dụng sự kiện jQuery Trigger cho trình xử lý sự kiện Change?
- 26. Tại sao các hàm không ẩn danh được thực thi khi sử dụng trình xử lý sự kiện nhấp chuột?
- 27. Cách chuyển thông số vào mã xử lý sự kiện của tôi để in hình ảnh
- 28. đèo nhấp chuột sự kiện thông qua việc xem android
- 29. Cách xóa tất cả trình xử lý sự kiện nhấp trong Jquery
- 30. Làm mới trình xử lý sự kiện Javascript
Nhiều dấu tại dòng này \t - Phương pháp setOnClickListener (View.OnClickListener) trong các loại Xem không áp dụng cho các đối số (DialogInterface.OnClickListener mới() {}) \t - Loại DialogInterface.OnClickListener() {} mới phải triển khai phương thức trừu tượng kế thừa DialogInterface.OnClickListener.onClick (DialogInterface, \t int) – Jaseem
@Jaseem: Có vẻ như bạn có 'import' làm cho trình biên dịch của bạn diễn giải' OnClickListener' như một 'DialogInterface.OnClickListener'. Hãy thử xác định rõ ràng '(mới View.OnClickListener() {...});' –
Tôi nghĩ giải pháp dưới đây là tốt hơn. Đặt 'android: onClick =" imageClick "' trong XML có vẻ dễ hiểu hơn. Giải pháp LIKE ở trên hoạt động rực rỡ trên web và JavaScript. Có vẻ như Java đang cố gắng sao chép JavaScript. Nó sẽ không bao giờ là tốt. – Jaseem