java - 如何判断java/android中按钮 View 是否存在

标签 java android button view

我很难相信我在任何地方都找不到这个答案。我的代码似乎无法正确检查按钮 View 是否存在。这就是我所拥有的,非常感谢任何建议。谢谢!

Java 文件:

public class LayoutsActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    ActivateButtons();
}

void ActivateButtons()
{
    Button mainLayoutButton = (Button) this.findViewById(R.id.MainLayout);
    if(mainLayoutButton != null)
    {
        mainLayoutButton.setOnClickListener(new View.OnClickListener() {    
            public void onClick(View v) {
                setContentView(R.layout.main);
                ActivateButtons();
            }
        });
    }

    Button linearLayoutButton = (Button) this.findViewById(R.id.LinearLayout);
    if(linearLayoutButton != null)
    {
        linearLayoutButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.mylinearlayout);
                ActivateButtons();
            }
        });
    }

    Button tableLayoutButton = (Button) this.findViewById(R.id.TableLayout);
        tableLayoutButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                setContentView(R.layout.mytablelayout);
                ActivateButtons();
            }
        });


    Button frameLayoutButton = (Button) this.findViewById(R.id.FrameLayout);
    if(frameLayoutButton != null)
    {
        frameLayoutButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                setContentView(R.layout.myframelayout);
                ActivateButtons();
            }
        });
    }
    Button relativeLayoutButton = (Button) this.findViewById(R.id.RelativeLayout);
    if(relativeLayoutButton != null)
    {
        relativeLayoutButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                setContentView(R.layout.myrelativelayout);
                ActivateButtons();
            }
        });
    }

    Button absoluteLayoutButton = (Button) this.findViewById(R.id.AbsoluteLayout);
    if(absoluteLayoutButton != null)
    {
         absoluteLayoutButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                setContentView(R.layout.myabsolutelayout);
                ActivateButtons();
            }
        }); 
    }

    Button DynamicLayoutButton = (Button) this.findViewById(R.id.DynamicLayout);
    if(DynamicLayoutButton != null)
    {
        DynamicLayoutButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                LinearLayout dynamicLayout = new LinearLayout(LayoutsActivity.this);
                dynamicLayout.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams size=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

                final TextView mytext= new TextView(LayoutsActivity.this);
                mytext.setText("This is the dynamic layout");
                mytext.setTextSize(20);
                mytext.setGravity(Gravity.CENTER);

                dynamicLayout.addView(mytext, size);
                /* Button b= new Button(LayoutsActivity.this);
                b.setText("Click Here to go to Main Layout");
                dynamicLayout.addView(b, size); */

                Button b= new Button(LayoutsActivity.this);
                b.setText("Main Layout");
                b.setId(R.id.DynamicLayout);
                dynamicLayout.addView(b, size);

                //Step L
                Button linear = new Button(LayoutsActivity.this);
                b.setText("Linear Layout");
                b.setId(R.id.LinearLayout);
                dynamicLayout.addView(linear, size);

                Button table = new Button(LayoutsActivity.this);
                table.setText("Table Layout");
                table.setId(R.id.LinearLayout);
                dynamicLayout.addView(table, size);

                ActivateButtons();

                setContentView(dynamicLayout);
            }
        });
    }
}

}

XML 文件:每种布局都有一个,但都很相似

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Linear layout" />
        <!-- The layout lays out its children next to each other (horizontally or vertically)-->
    <Button
        android:id="@+id/TableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Table Layout" />
        <!-- The layout represents a table like structure where there are columns and rows for the children-->
    <Button
        android:id="@+id/FrameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Frame Layout" />
        <!-- The layout places the children on top of one another-->
    <Button
        android:id="@+id/RelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Relative Layout" />
        <!-- The layout positions its children relative to each other-->
    <Button
        android:id="@+id/AbsoluteLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Absolute Layout" />
        <!-- The layout has children with absolute coordinates-->
    <Button
        android:id="@+id/DynamicLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dynamic Layout" />
        <!-- The layout creates a second, new page -->
    <Button
        android:id="@+id/MainLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Main Layout" />
        <!-- The layout returns to the original layout which seems to be similar to the linear layout-->
</LinearLayout>

最佳答案

我有点困惑。如果您的按钮位于 XML 布局中,它怎么可能不存在呢?如果它不在 XML 中,您的 R.id.MainLayout 将无法编译。除非你说 MainLayout 是某种布局而不是按钮。这也会导致问题,因为您将其转换为按钮。这段代码可以编译并运行吗?

关于java - 如何判断java/android中按钮 View 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10399208/

相关文章:

java - 如何将 .min() 或 .max() 作为流的方法参数?

java - 允许未加引号的 SOAP 操作 JAX-WS

java - 接口(interface)之间的转换

android - 以相同的格式从 iOS 和 Android 获取设备语言

python - 如何将 2 个按钮并排放置?

javascript - 切换两侧按钮 css

html - 将按钮放入 Canvas 中

java - 将 jar 添加到 lib/ext 有任何限制吗?

java - onActivityResult中的requestCode和resultCode指的是什么?

java - 在 Android 上生成 Azure SAS token