代码之家  ›  专栏  ›  技术社区  ›  mitch

我的按钮在android studio中没有响应

  •  1
  • mitch  · 技术社区  · 8 年前

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />
    
    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:id="@+id/button"
        tools:onClick="topClick" />
    
    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:id="@+id/button2"
        tools:onClick="bottomClick" />
    

    对于我的java方法;

    public class MyActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_layout);
    
            //Testing the Toast and Log in action
            Toast.makeText(this, "Can you see me?", Toast.LENGTH_SHORT).show();
    
            Log.i("info", "Done creating the app");
        }
    
    
        //creating method topclick
        public void topClick(View v) {
            Toast.makeText(this, "Top button clicked", Toast.LENGTH_SHORT).show();
    
            Log.i("info", "The user clicked the top button");
        }
    
        //creating method bottomclick
        public void bottomClick(View v) {
            Toast.makeText(this, "Bottom button clicked", Toast.LENGTH_SHORT).show();
    
            Log.i("info", "the user clicked the bottom button");
        }
    
    }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   stkent    8 年前

    代替

    tools:onClick

    android:onClick

    对于两个按钮。

    有关帮助您理解为什么需要进行此更改的更多上下文,请阅读 tools namespace :

    Android Studio在工具命名空间中支持各种XML属性,这些属性支持设计时功能(例如在片段中显示哪个布局)或编译时行为(例如应用于XML资源的收缩模式)。当你构建应用程序时,构建工具会删除这些属性,因此不会影响你的APK大小或运行时行为。

        2
  •  0
  •   UmarZaii Abhishek Tandon    8 年前

    <Button
        android:text="Button"
        android:id="@+id/button1" />
    
    <Button
        android:text="Button"
        android:id="@+id/button2" />
    

    ACTIVITY.JAVA

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
    
        Button button01 = (Button)findViewById(R.id.button01);
        Button button02 = (Button)findViewById(R.id.button02);
    
        button01 .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyActivity.this, "This is button01", Toast.LENGTH_SHORT).show();
            }
        });
        button02 .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyActivity.this, "This is button02", Toast.LENGTH_SHORT).show();
            }
        });
    }