简体   繁体   English

如何获取Android中图像按钮的点击事件?

[英]how to get click event of image button in android?

Could you please tell me how to get click event of image button in android? 您能告诉我如何在Android中获取图片按钮的点击事件吗? Actually, I have one row in which I have textview and image button. 实际上,我有一排具有textview和image按钮。 I am able to get click event of row but I am not able to get click event of image button. 我可以获取行的点击事件,但不能获取图像按钮的点击事件。

Actually, I want to change the image when I click on the image button. 实际上,我想在单击图像按钮时更改图像。

I found the code how to change the image of image button: 我发现了代码如何更改图像按钮的图像:

how to change the image on click of imagebutton? 单击图像按钮如何更改图像?

but how will I apply this on fragment? 但是如何将其应用于片段?

Here is my code, I want to get the click event of image button and change the image: 这是我的代码,我想获取图像按钮的click事件并更改图像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/station_name"
        android:padding="10dp"
        android:textColor="#eee345"
        android:textAppearance="?android:textAppearanceLarge"
        />

    <ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/start"
        android:background="#00ffffff"
     />


</LinearLayout>

fragment.java 片段

public class Fragmentone  extends Fragment{

    ArrayList<String> name;
    boolean isPressed=false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        name=new ArrayList<String>();
        name.add("First Station");
        name.add("Second Station");


        ListView listView = (ListView) view.findViewById(R.id.list_view);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = (String) parent.getItemAtPosition(position);
                Toast.makeText(getActivity(), "Item : " + item, Toast.LENGTH_SHORT).show();
            }
        });

        ArrayAdapter arrayAdapter =new ArrayAdapter(getActivity(),R.layout.row_layout,R.id.station_name,name);
        listView.setAdapter(arrayAdapter);



        return view;
    }


}

your ImageButton has id 您的ImageButton有ID

"@+id/favorite"

use it to initiate the button in your fragment when onCreateView is called 调用onCreateView时,使用它来启动片段中的按钮

Imagebutton ib = (Imagebutton)findViewbyId(R.id. favorite);

next add an onclick listener to your button so you know when it's clicked and take appropriate action 接下来,将onclick侦听器添加到您的按钮,以便您知道何时单击它并采取适当的措施

ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // here you change the image
            }
        });

In the case of fragment you have to pass the view. 如果是碎片,则必须通过视图。

Example: 例:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);

    ImageButton imgBtn = (ImageButton)view.findViewbyId(R.id.favorite);

    imgBtn.setOnClickListener(new View.OnClickListener() {
                @Override 
                public void onClick(View v) {
                    // Do your stuff 
                } 
            }); 

          return view;
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM