禁用linearLayout

时间:2012-06-09 06:10:27

标签: android android-layout

在我的应用程序中,我动态生成两个linearlayouts并将它们添加到ScrollView。

每个LinearLayout都有一个Button和一个Edit Text,我想在单击第二个LinearLayout的Button时,第一个Linearlayout被禁用。

代码:

package com.integrated.mpr;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class Page1 extends Activity implements OnClickListener{

    int i;
    int[][] id = new int[pos][3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        int p =0;
        Log.d("value of ", ""+pos);
        for(int i =0;i<2;i++){
            for(int j =0;j<3;j++){
                id[i][j] = p;
                p++;
            }
        }

        //In each row 1st column is the id for button
        //2nd column id for edittext
        //3rd column id for Linearlayout

        ScrollView sv = new ScrollView(this);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);

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

            LinearLayout llay = new LinearLayout(this);
            llay.setOrientation(LinearLayout.VERTICAL);
            llay.setId(id[i][2]);

            EditText et = new EditText(this);
            et.setId(id[i][1]);

            Button stop = new Button(this);
            stop.setText("Submit");
            stop.setId(id[i][0]);
            stop.setOnClickListener(this);

            llay.addView(et);
            llay.addView(stop);
            ll.addView(llay);

        }

        sv.addView(ll);
        this.setContentView(sv);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(v.getId()==id[1][0]){
            //when te second Button is clicked
            // now here disable the 1st linear layout
            //Can't think how to do it
        }

        }
}

2 个答案:

答案 0 :(得分:0)

将您的布局的引用保持为类成员,并在OnClick方法中将布局的visibility设置为View.GONE,以用于要禁用的布局。

答案 1 :(得分:0)

获取LinearLayout并将其设置为View.GONE

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if(v.getId()==id[1][0]){
        LinearLayout ll = (LinearLayout) findViewById(id);
        ll.setVisibility(View.GONE);
    }

    }