简体   繁体   中英

Android - Call Method With A Button From XML

I've got an app based on Sliding Tabs layout. I want to call the addMedicine method in the PageFragment class from an XML layout with a Button . The issue is that i cannot assign the method to the Button onClick event.

Here's the PageFragment class:

public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";

private int mPage;

// TEMP VARIABLES //
ListView ItemsLst;
String[] Items = {"Medicine 1", "Medicine 2", "Medicine 3"};
TextView output;

EditText nameFld;
EditText formatFld;
EditText amountFld;
EditText exp_dateFld;
EditText timeFld;

DBManager dbAdapt = new DBManager(getActivity());

public static PageFragment newInstance(int page) {
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, page);
    PageFragment fragment = new PageFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPage = getArguments().getInt(ARG_PAGE);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = null;
    switch(mPage)
    {
        case 1:
            view = inflater.inflate(R.layout.layout_sqltest, container, false);

            nameFld = (EditText) view.findViewById(R.id.nameFld);
            formatFld = (EditText) view.findViewById(R.id.formatFld);
            amountFld = (EditText) view.findViewById(R.id.amountFld);
            exp_dateFld = (EditText) view.findViewById(R.id.exp_dateFld);
            timeFld = (EditText) view.findViewById(R.id.timeFld);

            return view;
        case 2:
            view = inflater.inflate(R.layout.layout_settings, container, false);
            return view;
        case 3:
            view = inflater.inflate(R.layout.layout_info, container, false);
            return view;
    }
    return view;
}

public void addMedicine() // The method i want to call
{
    String name = nameFld.getText().toString();
    String format = formatFld.getText().toString();
    int amount = Integer.parseInt(amountFld.getText().toString());
    String exp_date = exp_dateFld.getText().toString();
    String time = timeFld.getText().toString();

    long id = dbAdapt.addMedicine(name, format, amount, exp_date, time);

}
}

And here is the layout_sqltest XML file:

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


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name"
        android:id="@+id/nameLbl"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nameFld"
        android:layout_gravity="center_horizontal"
        android:inputType="text" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Format"
        android:id="@+id/formatLbl"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/formatFld"
        android:layout_gravity="center_horizontal"
        android:inputType="text" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Amount"
        android:id="@+id/amountLbl"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/amountFld"
        android:layout_gravity="center_horizontal"
        android:inputType="text" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Expiration Date"
        android:id="@+id/exp_dateLbl"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/exp_dateFld"
        android:layout_gravity="center_horizontal"
        android:inputType="text" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Time"
        android:id="@+id/timeLbl"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/timeFld"
        android:layout_gravity="center_horizontal"
        android:inputType="text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Medicine"
        android:id="@+id/addMedicineBtn"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

The addMedicineBtn is the button i want to call the method with. Someone can help me? I really need that! Thank you in advance!

EDIT: I tried giving to addMedicine method the View param, but it still doesn't appear in the available methods for the addMedicineBtn in the onClick properties (I'm using Android Studio).

尝试使用 onClickListener

view.findViewById(R.id.addMedicineBtn).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { addMedicine(); } });

You must add tools:context="PageFragment" line to the container ( LinearLayout ) of PagerFragment 's layout file ( layout_sqltest ). Then onClick() method will find methods in PagerFragment class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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