按钮setOnClickListener不使用许多布局

时间:2016-09-11 08:59:02

标签: java android xml

我已经制作了许多LinearLayout,因为我想让它适合任何尺寸的设备。但问题是,当我点击我放在布局中的按钮时,setOnClickListener无效。

这是我的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> 

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

2 个答案:

答案 0 :(得分:0)

如果&#34;不工作&#34;表示您没有看到Toast,那是因为您没有在您创建的show()上调用Toast

onClick()更改为

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

答案 1 :(得分:0)

实际上您忘记在Toast中呼叫show()

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

像你一样在你的Toast中添加.show()

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