2012-06-09 12 views
-1

在我的應用程序中,我動態生成兩個線性佈局並將它們添加到ScrollView。禁用LinearLayout

每個LinearLayout都有一個Button和一個Edit Text,我希望當第二個LinearLayout的按鈕被點擊時,第一個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 
     } 

     } 
} 
+0

你有沒有考慮到保持佈局的引用作爲一個類的成員? –

+0

不,實際上我已經發布了兩個線性佈局的問題,但實際上在我的應用程序中,線性佈局的數量是可變的,所以也不會修復類成員 –

+0

@kumer piyush:您是否考慮過數組或列表? –

回答

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); 
    } 

    } 
+0

一個問題,能見度走了工作但是,當我使用ll.setEnabled(false)它不會被禁用..可能是什麼原因? –

+1

hide!=禁用 –

+0

禁用LinearLayout實際上不會執行任何操作。禁用使得用戶無法與對象交互,但他們仍然可以看到它。 –

0

保持你的佈局的參考作爲類成員和OnClick方法設置佈局visibilityView.GONE爲要禁用的佈局。

+1

hide!=禁用 –

+0

然後你想要在禁用模式下「用戶不能在佈局的子視圖中採取任何操作」 –

+0

確實,_user無法採取任何操作_但仍然可以看到視圖。 –

相關問題