如何在另一个xml列表视图下调用android:onClick xml按钮?

问题描述 投票:0回答:1

我尝试过这种方法,并在HomeFragment.java中将home.xml充气:

    public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.home, container, false);
        return root;
    }

    public void OnClickOnTransit(View v){
        final Button n = (Button) v;
        final String id = n.getTag().toString();

        AlertDialog.Builder builderSingle = new AlertDialog.Builder(getContext());

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1);
        arrayAdapter.add("Tag as On Transit");
        arrayAdapter.add("Cancel");


        builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String strOption = arrayAdapter.getItem(which);
                if(strOption.equalsIgnoreCase("Tag as On Transit"))
                {
                    Tag_as_on_transit(id);
                }
                else if(strOption.equalsIgnoreCase("Cancel"))
                {
                    //confirmViewLeveling(emp_tag[1],emp_name.getText().toString());
                }
                else
                {

                }
            }
        });
        builderSingle.show();

    }

这是我的home.xml,放在我的列表视图中。

<ListView
    android:id="@+id/lv_customer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

这是我的按钮所在的nested_listview.xml。

<Button
    android:id="@+id/btn_ontransit"
    android:layout_width="40dip"
    android:layout_height="40dip"
    android:background = "@drawable/ic_local_shipping_orange_24dp"
    android:textColor="#ff4500"
    android:textStyle="bold"
    android:text=""
    android:textSize="25sp"
    android:onClick="OnClickOnTransit"/>

我想要的是从nested_listview.xml调用OnClickOnTransit。预先感谢!

java android fragment
1个回答
0
投票
Button btn_conferma = (Button) view.findViewById(R.id.btn_conferma); btn_conferma.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do something } });
© www.soinside.com 2019 - 2024. All rights reserved.