2015-02-05 75 views
-3

我在我的android xml中添加了一個開關,我也有一些Edit_text框,在同一個活動中說有9個。我的目標是讓他們只需點擊一下即可看到。在這裏,我去Java代碼:爲什麼切換在android佈局不起作用在我的代碼?

public class MainActivity extends ActionBarActivity { 

//static int count = 1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    Switch mySwitch = (Switch)findViewById(R.id.switchButton); 
    mySwitch.setChecked(true); 
    mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if(isChecked) 
      { 
       LinearLayout pop = (LinearLayout)findViewById(R.id.linear); 
       pop.setVisibility(View.VISIBLE); 
      } 

      else 
      { 
       LinearLayout pop = (LinearLayout)findViewById(R.id.linear); 
       pop.setVisibility(View.INVISIBLE); 
      } 

     } 
    }); 



} 

而問題是,當我在設備/模擬器中運行這個我無法改變開關的狀態。任何幫助將不勝感激 。 :)

XML代碼:

<RelativeLayout xmlns:> 


    <Switch 
    android:id="@+id/switchButton" 

    android:gravity="center" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:switchMinWidth="14.5sp" 
    android:switchPadding="14.5sp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

    <ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 

    android:id="@+id/linear" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 



    <EditText 
     android:id="@+id/editTextOne" 
     android:layout_marginTop="150dp" 
     android:background="#1ec0e9" 
     android:alpha="0.5" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

    <EditText 
     android:id="@+id/editTextTwo" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:alpha="0.6" 
     android:background="#cacaca" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     /> 
    <EditText 
     android:id="@+id/editTextThree" 
     android:background="#1ec0e9" 
     android:alpha="0.5" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

    <EditText 
     android:id="@+id/editTextFour" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:alpha="0.6" 
     android:background="#cacaca" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     /> 

    <EditText 
     android:id="@+id/editTextFive" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#1ec0e9" 
     android:alpha="0.5" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     /> 
    <EditText 
     android:id="@+id/editTextSix" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:alpha="0.6" 
     android:background="#cacaca" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     /> 
    <EditText 
     android:id="@+id/editTextSeven" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#1ec0e9" 
     android:alpha="0.5" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     /> 
    <EditText 
     android:id="@+id/editTextEight" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:alpha="0.6" 
     android:background="#cacaca" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     /> 
    <EditText 
     android:id="@+id/editTextNine" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#1ec0e9" 
     android:alpha="0.5" 
     android:ems="13" 
     android:gravity="left" 
     android:typeface="monospace" 
     /> 



    </LinearLayout> 
    </ScrollView> 


</RelativeLayout> 
+0

安置自己的XML佈局.. – 2015-02-05 17:22:54

+0

你爲什麼初始化''pop'兩次LinearLayout'?你可以在'onCreate'中只初始化一次。 – iRuth 2015-02-05 17:26:53

+0

我的意思是說完整的佈局,我想看看有沒有隻有一個父佈局,或者你把你的孩子的意見,即編輯文本在父佈局下的另一個佈局。 – 2015-02-05 17:29:09

回答

1

第一個選項:

只是LinearLayout中

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 


    Your remaining views 

</LinearLayout> // Ending Parent Linear Layout 

第二個選項,請更改父RelativeLayout的:

如果你不想改變你的佈局從相對線性,只要這樣做

而不是將您的editfield設置爲margin_top = 150dp將其設置爲您的ScrollView。

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="150dp" 
    android:fillViewport="true" > 

你的開關開始工作

+0

請嘗試讓我知道,這是解決您的問題... – 2015-02-05 17:42:50

+0

它的工作,你能告訴我爲什麼它沒有工作時,它是相對的? – Roma 2015-02-05 17:50:36

+0

其實你的滾動視圖是其重疊你的開關的真正罪魁禍首,爲什麼你不能觸發交換機上的任何事件 – 2015-02-05 18:01:58

相關問題