Android按钮不会打印出字符串消息

时间:2022-06-07 01:46:39

I have tried to search in android developers but I had no luck.

我试图在Android开发人员中搜索,但我没有运气。

My onClick method does not print any message. I want to change my text from ... to ...; however nothing happens when I click it.

我的onClick方法不打印任何消息。我想把我的文字从...改为......;但是当我点击它时没有任何反应。

JAVA

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * This method is called when the Cookie button is clicked.
     */
    public void eatCookie(View view) {
        display("I'm so full");
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(String status) {
        TextView statusTextView = (TextView) findViewById(
                R.id.status_text_view);
        statusTextView.setText("" + status);
    }
}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B388FF"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/android_cookie_image_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleType="centerCrop"
        android:src="@drawable/before_cookie" />

    <TextView
        android:id="@+id/status_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:text="I'm so hungry"
        android:textColor="@android:color/white"
        android:textSize="34sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="EAT COOKIE"
        android:onClick="eatCookie"/>

</LinearLayout>

1 个解决方案

#1


1  

Your code is working correct in my device Try to Clean your project and re run the code again.

您的代码在我的设备中正常运行尝试清理项目并重新运行代码。

Build -> Clean

建立 - >清洁

and then run the code

然后运行代码

#1


1  

Your code is working correct in my device Try to Clean your project and re run the code again.

您的代码在我的设备中正常运行尝试清理项目并重新运行代码。

Build -> Clean

建立 - >清洁

and then run the code

然后运行代码