2009-12-03 33 views

Trả lời

75
ImageView img = (ImageView) findViewById(R.id.myImageId); 
img.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     // your code here 
    } 
}); 
+0

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

+0

@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() {...});' –

+0

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

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