Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未知实体Id_Java_Android - Fatal编程技术网

Java 未知实体Id

Java 未知实体Id,java,android,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.*

我在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. *;
代码:


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

1。与听众一起

假设您拥有以下XML文件:

main.xml


2。直接命名方法

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

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

此方法必须在活动/片段中实现,在该片段中,您已将此按钮充气(使用
设置内容视图
方法)

main.xml

希望这对你有帮助


干杯

问题出在XML中。
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();

    }

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