0

我想知道位置,使用标签位于 GridView 中的按钮的位置,并在获取位置后,使用位置从该按钮获取字母。这是代码

public View getView(final int position, View convertView, ViewGroup arg2) {

    View v = convertView;
    if (v == null) 
    {  
         LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = vi.inflate(R.layout.buttonlist, null);
    } 

    final Button btn= (Button)v.findViewById(R.id.letterbtn);
    btn.setText(word[position]+"");
    btn.setOnClickListener(new OnClickListener(){
        public void onClick(View v) 
        {
            btn.setTag(btn);
            String tag=btn.getTag().toString();

            btn.setVisibility(View.GONE);
        }

    });

    return v; 
}
4

1 回答 1

1

尝试这个:

 final Button btn= (Button)v.findViewById(R.id.letterbtn);
  // btn.setText(word[position]+"");
     btn.setTag(position);
     btn.setOnClickListener(new OnClickListener(){
       public void onClick(View v) 
       {
         // btn.setTag(btn);
         String pos=btn.getTag().toString();
         //You get a position of that button
         Log.e("position",pos); 
         //You get a word of that position
         Log.e("word",""+word[Integer.parseInt(pos)]); 

         btn.setVisibility(View.GONE);
    }

});
于 2012-07-05T07:16:35.967 回答