2015-02-05 2 views
-3

Я добавил переключатель в свой андроид xml, у меня также есть несколько блоков Edit_text, скажем 9 в том же действии. Моя цель - сделать их видимыми одним щелчком мыши. И вот идет мой код Java:Почему в моем коде не работает макет андроида?

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

Post ваш макет XML .. –

+0

Почему вы инициализации '' pop' LinearLayout' дважды? Вы можете инициализировать его только один раз в своем 'onCreate'. – iRuth

+0

Я хочу сказать, что полная компоновка, я хочу видеть, что есть только один макет родителя или вы помещаете свои дочерние представления, то есть редактируете текст в другом макете в разделе «Родительский макет». –

ответ

1

Первый вариант:

Просто измените свой родительский RelativeLayout с 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 

Второй вариант:

Если вы не хотите измените свой макет из Относительно Линейного, просто сделайте это

Вместо того, чтобы задавать margin_top = 150dp для вашего поля редактирования, установите его в свой ScrollView.

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

Коммутатор начинает работать

+0

Попробуйте и дайте мне знать, это разрешает вашу проблему ... –

+0

Это сработало, можете ли вы сказать мне, почему это не сработало, когда оно было относительно? – Roma

+0

Фактически, ваш вид прокрутки является фактическим виновником его перекрытия вашего коммутатора, поэтому вы не можете запускать какое-либо событие на коммутаторе –

Смежные вопросы