【问题标题】:How to create Button Object which is in another layout xml file in Android如何在Android的另一个布局xml文件中创建按钮对象
【发布时间】:2014-02-22 06:21:37
【问题描述】:

您好,我已经创建了一个 Android 应用程序,其中我使用 CustomAdapter 在 ListView 中添加了一些按钮和图像。

我的文件结构是:

layout
   [1] album ->List View
   [2] album_list -> Button

那么我如何创建按钮对象?

我的代码是:

专辑.xml

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="1" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_column="0"
        android:layout_gravity="left|top"
        android:layout_row="0" >
    </ListView>
</GridLayout>

专辑列表.xml

<RelativeLayout
            android:id="@+id/RelativeLayout001"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="2"
            android:orientation="horizontal" >



            <Button
                android:id="@+id/btn_eng"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="100px"
                android:layout_height="31px"
                android:layout_marginLeft="11dp"
                android:layout_marginRight="17dp"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="35dp"
                android:layout_alignTop="@+id/btn_hindi"                    
                android:layout_alignBottom="@+id/ImageView12"
                android:layout_alignLeft="@+id/ImageView12"
                android:background="@drawable/normal"
                android:text="ENG"
                android:textColor="@android:color/white" />

            <TextView
                android:id="@+id/tv_albumname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/tv_go"
                android:layout_alignTop="@+id/iv_album_image"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="2dp"
                android:text="Album Name"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@android:color/white" />

            <Button
                android:id="@+id/btn_hindi"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="100px"
                android:layout_height="31px"
                android:layout_marginTop="7dp"
                android:layout_marginRight="17dp"                   
                android:layout_alignTop="@+id/ImageView12"                  
                android:layout_alignLeft="@+id/btn_eng"
                android:background="@drawable/normal"
                android:text="HINDI"

                android:textColor="@android:color/white" />

        </RelativeLayout>

相册.java

Album_List_Custom_Adapter adapter =
            new Album_List_Custom_Adapter(getApplicationContext(), albumList);      
    lv.setAdapter(adapter);

Album_List_Custom_Adapter.java

private Context context;
ArrayList<HashMap<String, String>> listAlbum;

ViewHolder vholder;

private OnClickListener listener;


public Album_List_Custom_Adapter(Context context, ArrayList<HashMap<String, String>> albumList) 
{
    this.context = context;     
    this.listAlbum=albumList;
}

@Override
public int getCount() 
{
    return listAlbum.size();
}

@Override
public Object getItem(int position) 
{
    return null;
}

@Override
public long getItemId(int position) 
{
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
    View vi=convertView;

    if (convertView == null) 
    {           
        vi = inflater.inflate(R.layout.home_list_model, null);
        vholder = new ViewHolder();

        vholder.hindi=(Button)vi.findViewById(R.id.btn_hindi);
        vholder.eng=(Button)vi.findViewById(R.id.btn_eng);                      

        vholder.tv_album_name = (TextView) vi.findViewById(R.id.tv_albumname);

        vi.setTag(vholder);
    }
    else 
    {        
        vholder = (ViewHolder) (vi.getTag());
    }

    vholder.hindi.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            vholder.hindi.setBackgroundResource(R.drawable.selected);
            vholder.eng.setBackgroundResource(R.drawable.normal);
        }
    });

    vholder.hindi.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            v.setBackgroundResource(R.drawable.selected);
            vholder.eng.setBackgroundResource(R.drawable.normal);
        }
    });

    vholder.eng.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            v.setBackgroundResource(R.drawable.selected);
            vholder.hindi.setBackgroundResource(R.drawable.normal);
        }
    });

    vholder.tv_album_name.setText(listAlbum.get(position).get("album"));

    return vi;
} 

static class ViewHolder 
{
    TextView tv_album_name;
    Button eng, hindi;
}

【问题讨论】:

  • 在适配器中创建按钮对象,您可以在其中膨胀列表项布局。
  • 我还想在 Button 上设置 Click Listener。怎么样?
  • 添加按钮 b=(Button) view.findViewById(R.id.button);

标签: android xml button layout


【解决方案1】:

您必须在适配器中创建按钮对象,您可以在其中膨胀列表项布局。

喜欢

  Button b = (Button)yourViewobject.findViewById(R.id.button1);

现在为此实现 onClick 事件

  b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

编辑: 用这个。在 drawable 文件夹中创建一个名为 mybutton.xml

的自定义文件
   <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:drawable="@drawable/selected" android:state_selected="true"></item>

   <item android:drawable="@drawable/normal"></item>

   </selector>

现在只需在自定义布局文件中为两个按钮设置背景。

   android:background="@drawable/mybutton" // for Hindi Button

和英文按钮一样。

现在还要从适配器中删除 onClick 事件方法。

需要改变..

    <Button
            android:id="@+id/btn_eng"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="100px"
            android:layout_height="31px"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="17dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="35dp"
            android:clickable = "true"
            android:focusable = "true"
            android:layout_alignTop="@+id/btn_hindi"                    
            android:layout_alignBottom="@+id/ImageView12"
            android:layout_alignLeft="@+id/ImageView12"
            android:background="@drawable/mybutton"
            android:text="ENG"
            android:textColor="@android:color/white" />

【讨论】:

  • 它正在工作......但我的要求是当我点击一个按钮时,其他按钮的背景效果会改变。如何让它成为可能?
  • 所以他们没有改变?
  • 已更改,但有许多具有相同 ID 的按钮,因此当我单击 Button 时,另一个按钮的背景图片未更改。当前按钮的背景更改成功。
  • 但是将它们分配给不同的 id 并找到与其他按钮相同的 id 以便更改。
  • 将其添加到您已膨胀的自定义 xml 文件中
【解决方案2】:

在您的自定义适配器中执行此操作

@Override
      public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
         ViewHolder viewHolder;
       if (convertView == null) {

        convertView = inflater.inflate(R.layout.list_item, null);
        viewHolder=new ViewHolderforGroup();
           viewHolder.btn = (Button)convertView.findViewById(R.id.btn);
          convertView.setTag(viewHolder)
       }else{
           viewHolder = (ViewHolder)convertView.getTag(viewHolder);
       }

       viewHolder.btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {


                }
            });


    }

    static class ViewHolder{
      Button btn;

    }

【讨论】:

  • 它正在工作......但我的要求是当我点击一个按钮时,其他按钮的背景效果会改变。如何让它成为可能?
  • vholder.btn_hindi.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { v.setBackgroundResource(R.drawable.selected); vholder.btn_eng.setBackgroundResource(R.drawable.正常); } });
  • @SagarZala vholder.btn_eng.setBackgroundResource 在您的 xml 中吗?
  • 我总共有 20 个按钮,其中 10 个以 Hindi 命名,另外 10 个以 'Eng` 命名。所以当我点击任何一个按钮时,我想改变另一个按钮的背景。
【解决方案3】:
  public class CustomAdapter  extends BaseAdapter
{

private Context mContext;
Cursor cursor;
public CustomAdapter(Context context,Cursor cur) 
{
        super();
        mContext=context;
        cursor=cur;

}

public int getCount() 
{
    // return the number of records in cursor
    return cursor.getCount();
}

// getView method is called for each item of ListView
public View getView(int position,  View view, ViewGroup parent) 
{
                // inflate the layout for each item of listView
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.listview_each_item, null);


                                    // fetch the sender number and sms body from cursor
                String Number="123456";

                // get the reference of textViews
                TextView Number=(TextView)view.findViewById(R.id.textViewSMSSender);
                Button btn=(Button)view.findViewById(R.id.button);

                // Set the Sender number and smsBody to respective TextViews 
                Number.setText(senderNumber);
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});


                return view;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多