如何使用xml按钮以编程方式禁用按钮

时间:2017-01-09 08:09:44

标签: android xml android-layout for-loop

我有4个按钮在for循环中以编程方式创建按钮,是否可以通过使用xml中的按钮创建禁用onclicklistener函数或ontouchlistener函数?对不起我的英语,希望能理解我的问题。

样本图像

enter image description here

代码

public class Tab1 extends Fragment {

    TextView textView, test1;
    Button button, button2;
    LinearLayout rl;

    //Overriden method onCreateView
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View h = inflater.inflate(R.layout.tab1, container, false);
        //Returning the layout file after inflating
        //Change R.layout.tab1 in you classest
        textView = (TextView) h.findViewById(R.id.textView);
        test1 = (TextView) h.findViewById(R.id.test1);
        button = (Button) h.findViewById(R.id.button);
        button2 = (Button) h.findViewById(R.id.button2);
        rl = (LinearLayout) h.findViewById(R.id.tlayout);

        for (int i = 1; i < 5; i++) {

            final Button btnAddARoom = new Button(getActivity());
            RelativeLayout.LayoutParams params;
            params = new RelativeLayout.LayoutParams
                    (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            btnAddARoom.setText("Add");
            btnAddARoom.setLayoutParams(params);
            rl.addView(btnAddARoom);


            btnAddARoom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    test1.setText("qweqweqweqwe");
                }
            });

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    btnAddARoom.setOnClickListener(null);
                }
            });
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    test1.setText("111111111111111");
                }
            });
        }


        return h;
    }
}

4 个答案:

答案 0 :(得分:1)

将您在for循环中创建的所有按钮添加到数组中。

ArrayList<Buttons> buttons = new ArrayList<Buttons>();

for(int i =1 ; i<5 ; i++) {

    final Button btnAddARoom = new Button(getActivity());
    RelativeLayout.LayoutParams params;
    params = new RelativeLayout.LayoutParams
            (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    btnAddARoom.setText("Add");
    btnAddARoom.setLayoutParams(params);
    rl.addView(btnAddARoom);

    buttons.add(YourDynamicallyCreatedButton)
}

现在在你点击xml按钮:

onClick(){
   for(Button button: buttons){
      button.setEnable(false)
   }
}

注意:

当我们通过xml添加视图(按钮或文本..)时,我们会给它一个 id 。我们可以使用此ID访问这些元素。同样,当我们动态创建按钮时,我们需要将id设置为按钮/视图。

答案 1 :(得分:0)

setEnable(false)参考号

上致电Button

示例:

Button b ;
..
..
b.setEnable(false);

答案 2 :(得分:0)

继续参考以编程方式添加的按钮:

Button[] buttons = new Buttons[5];
for(int i =0 ; i<buttons.length ; i++) {
    buttons[i] = new Button(getActivity());
    final Button btnAddARoom = buttons[i];
    RelativeLayout.LayoutParams params;
    params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    btnAddARoom.setText("Add");
    btnAddARoom.setLayoutParams(params);
    rl.addView(btnAddARoom);
    btnAddARoom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            test1.setText("qweqweqweqwe");
        }
    });

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnAddARoom.setOnClickListener(null);
        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            test1.setText("111111111111111");
        }
    });
}

现在,当您要禁用这些按钮时:

private disableButtons() {
    for(int i=0;i<buttons.length;i++) {
        buttons[i].setEnabled(false);
    }
}

希望它会对你有所帮助!

答案 3 :(得分:0)

如果您有四个按钮的专用父级,那么您可以删除Arralist或数组。

string value  |  decimal result
--------------|----------------
 "< 0.22"     |   0.219
 "< 32.45"    |   32.449
 "> 2.0"      |   2.01
 "> 5"        |   5.1