未知实体ID

时间:2015-05-04 21:20:43

标签: java android

我在main.XML中创建了一个普通按钮,在Java中我添加了一个Toast消息,点击它。我的问题是我需要在点击时隐藏它,但这涉及R.id.main或类似的东西。问题是我收到错误"未知实体' id'"。我错过了进口吗?

进口:

package com.redstonelamp.DroidRedstoneLamp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View. *;

代码:

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void confirmClicked(View view){
        Toast.makeText(getApplicationContext(), "Please choose a version to download",
                       Toast.LENGTH_LONG).show();

    }

}

1 个答案:

答案 0 :(得分:2)

如果您想隐藏按钮,您有两种可能:

<强> 1。使用听众

我们假设你有这个XML文件:

  

main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/my_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

在您的主要课程中,您需要检索按钮,并将 MainActivity 订阅到此按钮的点击事件

  

MainActivity

    public class MainActivity extends Activity implements View.onClickListener
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            // this will inflate all the UI elements you have added
            // in your main.xml file
            setContentView(R.layout.main);

            // here you retrieve the button in the layout by its ID
            Button myButton = findViewById(R.id.my_button);

            // here you tell your button he should propagate the
            // Click event to this Activity.
            myButton.setOnClickListener(this);
        }

        @Override
        public void onClick(View view)
        {
            // here you handle the click
            // the view parameter is the view that was clicked
            // therefore your button :)
            // so all you have to do is to set it's visibility
            view.setVisibility(View.INVISIBLE);
        }
    }

<强> 2。直接命名方法

背后的机制是相同的,但是这个允许您拥有自定义方法名称。为此,您必须在名为onClick的XML文件中添加一个字段。

此字段将包含将处理点击的方法的名称。

此方法必须在活动 / 片段中实施,您已在此按钮(使用setContentView法)。

  

main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/my_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="myOnClickMethod" />

    </LinearLayout>

然后在你的MainActivity中:

  

MainActivity

    public class MainActivity extends Activity implements View.onClickListener
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            // this will inflate all the UI elements you have added
            // in your main.xml file
            setContentView(R.layout.main);
        }

        public void myOnClickMethod(View view)
        {
            // here you handle the click
            // the view parameter is the view that was clicked
            // therefore your button :)
            // so all you have to do is to set it's visibility
            view.setVisibility(View.INVISIBLE);
        }
    }

希望这会对你有所帮助。

干杯