Android Studio动态按钮创建

kdfy810k  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(267)

我是Android应用的新手,所以我的问题听起来很奇怪。我也尝试了一些其他的例子,但没有解决这个问题。
我试图创建动态按钮时,应用程序加载了以下代码:

package com.eft.positivelauncher.fragments;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import com.eft.positivelauncher.R;
import com.eft.libpositive.PosIntegrate;
import com.eft.positivelauncher.TransactionResponse;
import com.eft.positivelauncher.activities.MainActivity;
import com.eft.positivelauncher.weblink.WebLinkIntegrate;

import static com.eft.libpositive.PosIntegrate.CONFIG_TYPE.CT_AMOUNT_CASHBACK;
import static com.eft.libpositive.PosIntegrate.CONFIG_TYPE.CT_LANGUAGE;
import static com.eft.libpositive.PosIntegrate.TRANSACTION_TYPE.TRANSACTION_TYPE_REVERSAL;
import static com.eft.positivelauncher.activities.CustomActivity.hideKeyboard;
import static com.eft.positivelauncher.activities.MainActivity.CANCEL_TRANSACTION;
import static com.eft.positivelauncher.activities.MainActivity.COMPLETION;
import static com.eft.positivelauncher.activities.MainActivity.PREAUTH;
import static com.eft.positivelauncher.activities.MainActivity.REFUND;
import static com.eft.positivelauncher.activities.MainActivity.REVERSE_BY_UTI;
import static com.eft.positivelauncher.activities.MainActivity.REVERSE_LAST;
import static com.eft.positivelauncher.activities.MainActivity.SALE;
import static com.eft.positivelauncher.activities.MainActivity.lastReceivedUTI;
import static com.eft.libpositive.PosIntegrate.CONFIG_TYPE.CT_AMOUNT;
import static com.eft.libpositive.PosIntegrate.CONFIG_TYPE.CT_CANCELLED_TIMEOUT;
import static com.eft.libpositive.PosIntegrate.CONFIG_TYPE.CT_RRN;
import static com.eft.libpositive.PosIntegrate.CONFIG_TYPE.CT_UTI;
import static com.eft.libpositive.PosIntegrate.TRANSACTION_TYPE.TRANSACTION_TYPE_COMPLETION;
import static com.eft.libpositive.PosIntegrate.TRANSACTION_TYPE.TRANSACTION_TYPE_PREAUTH;
import static com.eft.libpositive.PosIntegrate.TRANSACTION_TYPE.TRANSACTION_TYPE_REFUND;
import static com.eft.libpositive.PosIntegrate.TRANSACTION_TYPE.TRANSACTION_TYPE_SALE;

public class InputAmountFragment extends Fragment {

    private static final String TAG="";
    private String screenTitle;
    private int trans_Type;
    public ArrayList<String> TableCosts = new ArrayList<String>();
    private static final int NUM_ROWS = 7;
    private static final int NUM_COLS = 10;

    Button TableNumbers = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
        screenTitle = this.getArguments().getString("Screen_Title");
        trans_Type = this.getArguments().getInt("Trans_Type");
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.input_amount_fragment, container, false);
    }

    @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        // Setup any handles to view objects here


        final EditText editTextAmount = view.findViewById(R.id.ed_enter_amount);
        final EditText editTextRRN = view.findViewById(R.id.ed_enter_rrn_number);
        final EditText editTextCancelTime = view.findViewById(R.id.ed_enter_cancel_time);

      final  GridLayout layout = view.findViewById(R.id.recycler_view);
       
        int i;
        double current =  2.99;
        for(i=0; i<10; i++){
            Button TableNumbers = new  Button();
           TableNumbers.setText("TABLE  "+i);
        TableNumbers.setHeight(25);
        TableNumbers.setTextColor(Color.RED);
          TableNumbers.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        TableNumbers.setId(i);
         current = current+ 19.99;
          TableCosts.add(i,""+current);
            TableNumbers.setOnClickListener(btnclick);
             layout.addView(TableNumbers);
        }

        InputMethodManager imm = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

        if (WebLinkIntegrate.enabled) {

            if(trans_Type == COMPLETION || trans_Type == REFUND) {
                editTextRRN.setVisibility(View.VISIBLE);
                editTextRRN.setHint("Enter ID Here");
                if (MainActivity.lastReceivedRRN != null && MainActivity.lastReceivedRRN.length() > 0)
                    editTextRRN.setText(MainActivity.lastReceivedRRN);
                if (MainActivity.lastReceivedAmount != null && MainActivity.lastReceivedAmount.length() > 0)
                    editTextAmount.setText(MainActivity.lastReceivedAmount);
                editTextRRN.setInputType(InputType.TYPE_CLASS_TEXT );

            } else if (trans_Type == REVERSE_LAST || trans_Type == REVERSE_BY_UTI) {
                if (MainActivity.lastReceivedAmount != null && MainActivity.lastReceivedAmount.length() > 0)
                    editTextAmount.setText(MainActivity.lastReceivedAmount);
            }
            //if(trans_Type == CANCEL_TRANSACTION) {
            //    editTextCancelTime.setVisibility(View.VISIBLE);
            //}
        } else {
            if(trans_Type == COMPLETION) {
                editTextRRN.setVisibility(View.VISIBLE);
            }
            if(trans_Type == CANCEL_TRANSACTION) {
                editTextCancelTime.setVisibility(View.VISIBLE);
            }
        }

        TextView textViewTitle = view.findViewById(R.id.tv_enter_amount);
        textViewTitle.setText(screenTitle);
        Log.e("TEXT TITLE",textViewTitle.toString());
        Button nextButton = view.findViewById(R.id.button_submit);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Hide Keyboard
                System.out.println("Hint:" + editTextAmount.getHint());
                hideKeyboard(view.getContext());
                HashMap<PosIntegrate.CONFIG_TYPE, String> args = new HashMap<PosIntegrate.CONFIG_TYPE, String>();
                args.put(CT_AMOUNT, editTextAmount.getText().toString());
                //args.put(CT_AMOUNT_CASHBACK, editTextAmount.getText().toString());
                args.put(CT_LANGUAGE, "en_GB");

                if (WebLinkIntegrate.enabled) {
                    switch (trans_Type) {
                        case SALE:
                            WebLinkIntegrate.executeTransaction(getActivity(), "SALE", args);
                            break;
                        case REFUND:
                            args.put(CT_RRN, editTextRRN.getText().toString());
                            WebLinkIntegrate.executeTransaction(getActivity(), "REFUND", args);
                            break;
                        case REVERSE_LAST:
                            WebLinkIntegrate.executeTransaction(getActivity(), "REVERSAL", args);
                            break;
                        case REVERSE_BY_UTI:
                            args.put(CT_UTI, lastReceivedUTI);
                            PosIntegrate.executeReversal(getActivity(), args);
                            break;
                        case PREAUTH:
                            WebLinkIntegrate.executeTransaction(getActivity(), "PREAUTH", args);
                            break;
                        case COMPLETION:
                            args.put(CT_RRN, editTextRRN.getText().toString());
                            WebLinkIntegrate.executeTransaction(getActivity(), "COMPLETION", args);
                            break;
                        case CANCEL_TRANSACTION:
                            args.put(CT_CANCELLED_TIMEOUT, editTextCancelTime.getText().toString());
                            PosIntegrate.executeTransaction(getActivity(), TRANSACTION_TYPE_SALE, args);
                            break;
                    }
                } else {

                    switch (trans_Type) {
                        case SALE:
                            PosIntegrate.executeTransaction(getActivity(), TRANSACTION_TYPE_SALE, args);
                            break;
                        case REFUND:
                            PosIntegrate.executeTransaction(getActivity(), TRANSACTION_TYPE_REFUND, args);
                            break;
                        case REVERSE_LAST:
                            PosIntegrate.executeReversal(getActivity(), args);
                            break;
                        case REVERSE_BY_UTI:
                            args.put(CT_UTI, lastReceivedUTI);
                            PosIntegrate.executeReversal(getActivity(), args);
                            break;
                        case PREAUTH:
                            PosIntegrate.executeTransaction(getActivity(), TRANSACTION_TYPE_PREAUTH, args);
                            break;
                        case COMPLETION:
                            args.put(CT_RRN, editTextRRN.getText().toString());
                            PosIntegrate.executeTransaction(getActivity(), TRANSACTION_TYPE_COMPLETION, args);
                            break;
                        case CANCEL_TRANSACTION:
                            args.put(CT_CANCELLED_TIMEOUT, editTextCancelTime.getText().toString());
                            PosIntegrate.executeTransaction(getActivity(), TRANSACTION_TYPE_SALE, args);
                            break;
                    }
                }

            }
        });
    }
    
}

然而Button TableNumbers = new Button();返回错误代码C:\positivelauncher\src\main\java\com\eft\positivelauncher\fragments\InputAmountFragment.java:92:错误:不兼容的类型:无法将InputAmountFragment转换为上下文
函数如下:

final  GridLayout layout = view.findViewById(R.id.recycler_view);
        int i;
        double current =  2.99;

        for(i=0; i<10; i++){

            Button TableNumbers = new  Button();
           TableNumbers.setText("TABLE  "+i);
        TableNumbers.setHeight(25);
        TableNumbers.setTextColor(Color.RED);
          TableNumbers.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
          TableNumbers.setId(i);
          current = current+ 19.99;
          TableCosts.add(i,""+current);
          TableNumbers.setOnClickListener(btnclick);
          layout.addView(TableNumbers);

        }

当我使用Button TableNumbers =null时;它给出了另一个错误。提前感谢:)

ergxz8rk

ergxz8rk1#

要以编程方式创建Button,请使用Button btn =new Button(requireContext());,因为它的构造函数需要一个上下文示例。

相关问题