何让动态创建的ImageView或TextView的点击ID或位

何让动态创建的ImageView或TextView的点击ID或位

本文介绍了如何让动态创建的ImageView或TextView的点击ID或位置的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建动态的ImageView和按钮,滚动型,所以我想这是动态创建我怎样才能得到这这里是我的code

为单击项目的ID

公共类测试活动扩展了活动实现OnClickListener
    {

 私有静态最后弦乐TAG_DATA =数据;
    私有静态最后弦乐TAG_ADVERTISE =advertisments;
    私有静态最后弦乐TAG_ADVERTISEID =advt_id;
    串advertiseid;    私有静态最后弦乐TAG_SHOWTEXT =showtext;
    串showtext;    私有静态最后弦乐TAG_PRODUCTINFO =产品的详细信息;
    串productinfo;    私有静态最后弦乐TAG_THUMBIMAGE =thumbsrc;
    串thumbimage;    私有静态最后弦乐TAG_DISTANCE =距离;
    字符串的距离;    私有静态最后弦乐TAG_STIPCIATED =stipciated;
    串stipciated;
    ArrayList的<&HashMap的LT;字符串,字符串>> listadvertise =新的ArrayList<&HashMap的LT;字符串,字符串>>();
    ArrayList的<&HashMap的LT;字符串,字符串>> listadvertise1 =新的ArrayList<&HashMap的LT;字符串,字符串>>();
    ArrayList的<&HashMap的LT;字符串,字符串>> listadvertise2 =新的ArrayList<&HashMap的LT;字符串,字符串>>();
    //为首页做广告Webservice的参数
    字符串URL;
    串fbid;
    串纬度;
    字符串的经度;
    串passdistance;
    字符串偏移;    //为stipciated做广告Webservice的参数    字符串userid;
    串stipciate;    INT screenheight;
    INT屏幕宽度;
    AlertDialog alertDialog;
    私人ProgressDialog progressDialog;
    ImageView的imagemenu;    滚动型scrollView3;
    ImageView的IM;
    的LinearLayout homelistlayout1;
    的LinearLayout homelistlayout2;
    公共静态最终诠释IMG = 50000;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        的setContentView(R.layout.test);        alertDialog =新AlertDialog.Builder(本).create();
        DisplayMetrics屏幕尺寸=新DisplayMetrics();
        。getWindowManager()getDefaultDisplay()getMetrics(屏幕大小)。        screenheight = screensize.heightPixels;
        屏幕宽度= screensize.widthPixels;        Log.e(屏幕高度, - >中+ screenheight);
        Log.e(屏幕宽度, - >中+屏幕宽度);
        RelativeLayout的headerlLayout =(RelativeLayout的)findViewById(R.id.headerlayout);
        headerlLayout.setLayoutParams(新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,((屏幕宽度* 8)/ 100)+10));        如果(CheckConnection.getInstance(本).isOnline(本))
        {        //新HomeAsyncTask()执行();        }
        其他
        {
            警报();
        }        imagemenu =(ImageView的)findViewById(R.id.imagemenu);
        imagemenu.setOnClickListener(新OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                    意图I =新意图(TestActivity.this,Horizo​​ntalActivity.class);
                    startActivity(ⅰ);
            }
        });      scrollView3 =(滚动型)findViewById(R.id.scrollview3);        scrollView3.post(新的Runnable(){            公共无效的run()
            {                scrollView3.scrollTo(0,200);
            }
        });
        homelistlayout1 =(的LinearLayout)findViewById(R.id.homelistlayout1);
        homelistlayout1.setPadding(0,100,0,0);
        homelistlayout2 =(的LinearLayout)findViewById(R.id.homelistlayout2);
        的for(int i = 0; I< 12;我++)
        {            IM =新ImageView的(TestActivity.this);
            im.setLayoutParams(新LinearLayout.LayoutParams(200,200));            如果(我%2 == 0)
            {                    im.setImageResource(R.drawable.adv);
                    im.setId(ⅰ);
                    homelistlayout1 =(的LinearLayout)findViewById(R.id.homelistlayout1);
                    homelistlayout1.addView(IM);
            }
            其他
            {
                im.setImageResource(R.drawable.adv2);
                im.setId(ⅰ);
                homelistlayout2 =(的LinearLayout)findViewById(R.id.homelistlayout2);
                homelistlayout2.addView(IM);                }            }
            im.setOnClickListener(新OnClickListener(){                公共无效的onClick(视图v){                    ImageView的IV =(ImageView的)V;                    Log.e(红藤, - >中+ iv.getId());                }
            });
            }
       }公共无效的onClick(视图v){    Log.e(点击,----->中+ v.getId());
    开关(v.getId())
    {
    案例IMG:
                    Log.e(点击,----->中+ v.getId());
            打破;    默认:
        打破;
    }}}


解决方案

在您的code就需要进行更改,


  1. 您必须添加 im.setOnClickListener(本); 在的的ImageView 循环

  2. 删除下面的方法

      im.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                ImageView的IV =(ImageView的)V;
                Log.e(红藤, - >中+ iv.getId());
            }
        });


  3. 覆盖的onClick()因为你已经实施 onClickcListener 在你的活动。


看看下面code,(和匹配与code知道实际的问题)

 公共类测试活动扩展了活动实现OnClickListener
{
    私有静态最后弦乐TAG_DATA =数据;
    私有静态最后弦乐TAG_ADVERTISE =advertisments;
    私有静态最后弦乐TAG_ADVERTISEID =advt_id;
    串advertiseid;
    私有静态最后弦乐TAG_SHOWTEXT =showtext;
    串showtext;
    私有静态最后弦乐TAG_PRODUCTINFO =产品的详细信息;
    串productinfo;    私有静态最后弦乐TAG_THUMBIMAGE =thumbsrc;
    串thumbimage;    私有静态最后弦乐TAG_DISTANCE =距离;
    字符串的距离;    私有静态最后弦乐TAG_STIPCIATED =stipciated;
    串stipciated;    ArrayList的<&HashMap的LT;字符串,字符串>> listadvertise =新的ArrayList<&HashMap的LT;字符串,字符串>>();
    ArrayList的<&HashMap的LT;字符串,字符串>> listadvertise1 =新的ArrayList<&HashMap的LT;字符串,字符串>>();
    ArrayList的<&HashMap的LT;字符串,字符串>> listadvertise2 =新的ArrayList<&HashMap的LT;字符串,字符串>>();
    //为首页做广告Webservice的参数
    字符串URL;
    串fbid;
    串纬度;
    字符串的经度;
    串passdistance;
    字符串偏移;    //为stipciated做广告Webservice的参数    字符串userid;
    串stipciate;    INT screenheight;
    INT屏幕宽度;
    AlertDialog alertDialog;
    私人ProgressDialog progressDialog;
    ImageView的imagemenu;
    滚动型scrollView3;
    私人的ListView listViewLeft;
    私人的ListView listViewRight;    INT [] leftViewsHeights;
    INT [] rightViewsHeights;    ImageView的IM;
    的LinearLayout homelistlayout1;
    的LinearLayout homelistlayout2;
     公共静态最终诠释IMG = 50000;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        的setContentView(R.layout.test);        alertDialog =新AlertDialog.Builder(本).create();
        DisplayMetrics屏幕尺寸=新DisplayMetrics();
        。getWindowManager()getDefaultDisplay()getMetrics(屏幕大小)。        screenheight = screensize.heightPixels;
        屏幕宽度= screensize.widthPixels;        Log.e(屏幕高度, - >中+ screenheight);
        Log.e(屏幕宽度, - >中+屏幕宽度);        RelativeLayout的headerlLayout =(RelativeLayout的)findViewById(R.id.headerlayout);
        headerlLayout.setLayoutParams(新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,((屏幕宽度* 8)/ 100)+10));        如果(CheckConnection.getInstance(本).isOnline(本))
        {        //新HomeAsyncTask()执行();
        }
        其他
        {
            警报();
        }        imagemenu =(ImageView的)findViewById(R.id.imagemenu);
        imagemenu.setOnClickListener(新OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                    意图I =新意图(TestActivity.this,Horizo​​ntalActivity.class);
                    startActivity(ⅰ);
            }
        });      scrollView3 =(滚动型)findViewById(R.id.scrollview3);
        scrollView3.post(新的Runnable(){
            公共无效的run()
            {
                scrollView3.scrollTo(0,200);
            }
        });        homelistlayout1 =(的LinearLayout)findViewById(R.id.homelistlayout1);
        homelistlayout1.setPadding(0,100,0,0);
        homelistlayout2 =(的LinearLayout)findViewById(R.id.homelistlayout2);        的for(int i = 0; I< 12;我++)
        {
            IM =新ImageView的(TestActivity.this);
            im.setLayoutParams(新LinearLayout.LayoutParams(200,200));
            im.setOnClickListener(本);
            如果(我%2 == 0)
            {
                    im.setImageResource(R.drawable.adv);
                    im.setId(ⅰ);
                    homelistlayout1 =(的LinearLayout)findViewById(R.id.homelistlayout1);
                    homelistlayout1.addView(IM);
            }
            其他
            {
                im.setImageResource(R.drawable.adv2);
                im.setId(ⅰ);
                homelistlayout2 =(的LinearLayout)findViewById(R.id.homelistlayout2);
                homelistlayout2.addView(IM);
            }
        }
       }
   }@覆盖
公共无效的onClick(视图v){
  Log.e(点击,----->中+ v.getId());
  开关(v.getId())
   {
    情况1:
        Log.e(点击,----->中+ v.getId());
        打破;
    案例2:
        打破;
    。
    。
    。
    默认:
    打破;
 }
}

i want to create dynamic imageview and button which in scrollview so i want to get the id for that clicked item which is created dynamically how can i get this here is my code

public class TestActivity extends Activity implements OnClickListener {

    private static final String TAG_DATA="data";
    private static final String TAG_ADVERTISE="advertisments";
    private static final String TAG_ADVERTISEID="advt_id";
    String advertiseid;

    private static final String TAG_SHOWTEXT="showtext";
    String showtext;

    private static final String TAG_PRODUCTINFO="product_info";
    String productinfo;

    private static final String TAG_THUMBIMAGE="thumbsrc";
    String thumbimage;

    private static final String TAG_DISTANCE="distance";
    String distance;

    private static final String TAG_STIPCIATED="stipciated";
    String stipciated;


    ArrayList<HashMap<String, String>> listadvertise = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> listadvertise1 = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> listadvertise2 = new ArrayList<HashMap<String,String>>();
    // Webservice parameter for home advertise
    String url;
    String fbid;
    String latitude;
    String longitude;
    String passdistance;
    String offset;

    // Webservice parameter for stipciated advertise

    String userid;
    String stipciate;

    int screenheight;
    int screenwidth;
    AlertDialog alertDialog;
    private ProgressDialog progressDialog;
    ImageView imagemenu;

    ScrollView scrollView3;


    ImageView im;
    LinearLayout homelistlayout1;
    LinearLayout homelistlayout2;
    public static final int img=50000;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test);

        alertDialog = new AlertDialog.Builder(this).create();
        DisplayMetrics screensize= new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(screensize);

        screenheight=screensize.heightPixels;
        screenwidth=screensize.widthPixels;

        Log.e("Screen Height","---->"+screenheight);
        Log.e("Screen Width ","---->"+screenwidth);


        RelativeLayout headerlLayout = (RelativeLayout)findViewById(R.id.headerlayout);
        headerlLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,((screenwidth*8)/100)+10));

        if(CheckConnection.getInstance(this).isOnline(this))
        {

        //  new HomeAsyncTask().execute("");

        }
        else
        {
            alert();
        }

        imagemenu=(ImageView)findViewById(R.id.imagemenu);
        imagemenu.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                    Intent i = new Intent(TestActivity.this,HorizontalActivity.class);
                    startActivity(i);
            }
        });



      scrollView3=(ScrollView)findViewById(R.id.scrollview3);

        scrollView3.post(new Runnable() {

            public void run()
            {

                scrollView3.scrollTo(0, 200);
            }
        });


        homelistlayout1=(LinearLayout)findViewById(R.id.homelistlayout1);
        homelistlayout1.setPadding(0, 100, 0, 0);
        homelistlayout2=(LinearLayout)findViewById(R.id.homelistlayout2);


        for(int i=0;i<12;i++)
        {

            im= new ImageView(TestActivity.this);
            im.setLayoutParams(new LinearLayout.LayoutParams(200, 200));

            if(i%2==0)
            {

                    im.setImageResource(R.drawable.adv);
                    im.setId(i);
                    homelistlayout1=(LinearLayout)findViewById(R.id.homelistlayout1);
                    homelistlayout1.addView(im);


            }
            else
            {
                im.setImageResource(R.drawable.adv2);
                im.setId(i);
                homelistlayout2=(LinearLayout)findViewById(R.id.homelistlayout2);
                homelistlayout2.addView(im);

                }

            }


            im.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                    ImageView iv=(ImageView) v;

                    Log.e("sfas","-->"+iv.getId());

                }
            });
            }
       }

public void onClick(View v) {

    Log.e("Clicked","----->"+v.getId());
    switch (v.getId())
    {
    case img:
                    Log.e("Clicked","----->"+v.getId());
            break;

    default:
        break;
    }

}

}
解决方案

Just required changes on your code,

  1. You have to add im.setOnClickListener(this); in for loop of ImageView.
  2. Remove below method

    im.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                ImageView iv=(ImageView) v;
                Log.e("sfas","-->"+iv.getId());
            }
        });
    

  3. Override onClick() As you have already implement onClickcListener in your Activity.

Look at below code, (And match with your code to know actual problem)

public class TestActivity extends Activity implements OnClickListener
{
    private static final String TAG_DATA="data";
    private static final String TAG_ADVERTISE="advertisments";
    private static final String TAG_ADVERTISEID="advt_id";
    String advertiseid;
    private static final String TAG_SHOWTEXT="showtext";
    String showtext;
    private static final String TAG_PRODUCTINFO="product_info";
    String productinfo;

    private static final String TAG_THUMBIMAGE="thumbsrc";
    String thumbimage;

    private static final String TAG_DISTANCE="distance";
    String distance;

    private static final String TAG_STIPCIATED="stipciated";
    String stipciated;

    ArrayList<HashMap<String, String>> listadvertise = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> listadvertise1 = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> listadvertise2 = new ArrayList<HashMap<String,String>>();
    // Webservice parameter for home advertise
    String url;
    String fbid;
    String latitude;
    String longitude;
    String passdistance;
    String offset;

    // Webservice parameter for stipciated advertise

    String userid;
    String stipciate;

    int screenheight;
    int screenwidth;
    AlertDialog alertDialog;
    private ProgressDialog progressDialog;
    ImageView imagemenu;
    ScrollView scrollView3;


    private ListView listViewLeft;
    private ListView listViewRight;

    int[] leftViewsHeights;
    int[] rightViewsHeights;

    ImageView im;
    LinearLayout homelistlayout1;
    LinearLayout homelistlayout2;
     public static final int img=50000;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test);

        alertDialog = new AlertDialog.Builder(this).create();
        DisplayMetrics screensize= new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(screensize);

        screenheight=screensize.heightPixels;
        screenwidth=screensize.widthPixels;

        Log.e("Screen Height","---->"+screenheight);
        Log.e("Screen Width ","---->"+screenwidth);

        RelativeLayout headerlLayout = (RelativeLayout)findViewById(R.id.headerlayout);
        headerlLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,((screenwidth*8)/100)+10));

        if(CheckConnection.getInstance(this).isOnline(this))
        {

        //  new HomeAsyncTask().execute("");
        }
        else
        {
            alert();
        }

        imagemenu=(ImageView)findViewById(R.id.imagemenu);
        imagemenu.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                    Intent i = new Intent(TestActivity.this,HorizontalActivity.class);
                    startActivity(i);
            }
        });

      scrollView3=(ScrollView)findViewById(R.id.scrollview3);
        scrollView3.post(new Runnable() {
            public void run()
            {
                scrollView3.scrollTo(0, 200);
            }
        });

        homelistlayout1=(LinearLayout)findViewById(R.id.homelistlayout1);
        homelistlayout1.setPadding(0, 100, 0, 0);
        homelistlayout2=(LinearLayout)findViewById(R.id.homelistlayout2);

        for(int i=0;i<12;i++)
        {
            im= new ImageView(TestActivity.this);
            im.setLayoutParams(new LinearLayout.LayoutParams(200, 200));
            im.setOnClickListener(this);
            if(i%2==0)
            {
                    im.setImageResource(R.drawable.adv);
                    im.setId(i);
                    homelistlayout1=(LinearLayout)findViewById(R.id.homelistlayout1);
                    homelistlayout1.addView(im);
            }
            else
            {
                im.setImageResource(R.drawable.adv2);
                im.setId(i);
                homelistlayout2=(LinearLayout)findViewById(R.id.homelistlayout2);
                homelistlayout2.addView(im);
            }
        }
       }
   }

@Override
public void onClick(View v) {
  Log.e("Clicked","----->"+v.getId());
  switch (v.getId())
   {
    case 1:
        Log.e("Clicked","----->"+v.getId());
        break;
    case 2:
        break;
    .
    .
    .
    default:
    break;
 }
}

这篇关于如何让动态创建的ImageView或TextView的点击ID或位置的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:13