2016-09-11 6 views
0

Ich habe viele LinearLayout gemacht, wie ich es mit jeder Größe des Geräts machen wollen. Aber das Problem ist jetzt, dass setOnClickListener nicht funktioniert, wenn ich auf die Schaltfläche klicke, die ich in die Layouts eingefügt habe.Button setOnClickListener funktioniert nicht mit vielen Layouts

Hier meine XML

 <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.52" 
      android:orientation="vertical" /> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.48" 
      android:orientation="vertical" > 
      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="0.65" 
       android:baselineAligned="false"> 
       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.64" 
        android:baselineAligned="false"> 
        <LinearLayout 
         android:orientation="horizontal" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_weight="0.7"/> 
        <LinearLayout 
         android:orientation="horizontal" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_weight="0.3"> 

         <Button 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:id="@+id/wordlist_btn" /> 
        </LinearLayout> 
       </LinearLayout> 
       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.36" 
        android:baselineAligned="false"> 
        <LinearLayout 
         android:orientation="horizontal" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_weight="0.2"> 
         <Button 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:id="@+id/livingroom_btn"/> 
        </LinearLayout> 
        <LinearLayout 
         android:orientation="horizontal" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_weight="0.8"/> 
       </LinearLayout> 
      </LinearLayout> 
      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="0.35" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

Mein Java

package app.magiscamp.meinhaus; 

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_main); 
     Button word_btn = (Button)findViewById(R.id.wordlist_btn); 
     Button living_btn = (Button)findViewById(R.id.livingroom_btn); 
     word_btn.setVisibility(View.VISIBLE); 
     living_btn.setVisibility(View.VISIBLE); 
     word_btn.setBackgroundColor(Color.TRANSPARENT);  
     living_btn.setBackgroundColor(Color.TRANSPARENT); 
     word_btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "Wordlist", Toast.LENGTH_SHORT); 
      } 
     }); 

     living_btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "Wordlist", Toast.LENGTH_SHORT); 
      } 
     }); 
    } 
} 

Antwort

0

ist Wenn "funktioniert nicht" bedeutet, dass Sie nicht die Toast s sehen, dann ist das, weil du bist nicht angerufen show() auf der Toast s Sie erstellt.

ändern die onClick() s so etwas wie

Toast.makeText(getApplicationContext(), "Wordlist", Toast.LENGTH_SHORT).show(); 
+0

nennen vergessen Vielen Dank ! funktioniert jetzt –

0

Eigentlich haben Sie show() in Toast

Toast.makeText(getApplicationContext(), "Wordlist", Toast.LENGTH_SHORT); 

hinzufügen .show() in Toast wie diese

Toast.makeText(getApplicationContext(), "Wordlist", Toast.LENGTH_SHORT).show(); 
Verwandte Themen