如何判断java/android中是否存在按钮视图
我很难相信我在任何地方都找不到这个答案。我的代码似乎不能正确地检查按钮视图的存在。这是我的建议,任何建议都非常感谢。谢谢 Java文件:如何判断java/android中是否存在按钮视图,java,android,button,view,Java,Android,Button,View,我很难相信我在任何地方都找不到这个答案。我的代码似乎不能正确地检查按钮视图的存在。这是我的建议,任何建议都非常感谢。谢谢 Java文件: public class LayoutsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Ac
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是一些布局而不是按钮。这也会造成问题,因为你把它扔到一个按钮上。。这段代码编译并运行了吗?是的,它编译并运行了,我需要检查当im在动态页面上时这些按钮是否存在。也许我从错误的角度看这个问题?也许我不确定你所说的存在是什么意思。如果它们有ID并且存在于XML中,那么它们就必须“存在”,不是吗?XML布局不是动态的。当然,您可以在以后动态地更改它们。但是如果您使用的是R.id,那么您就是在引用XML。你是说它们看得见吗?我想这可能是我搜索时的问题。因此,如果我创建了一个动态页面,但按钮不在上面,那么它们仍然存在。所以在打开动态页面后,我想检查它们的可见性。(在我的老师笔记中,它说检查他们是否存在,所以我不太确定)。是的,那会更有意义。但是,如果按钮在XML中,则它们不会为null。如果修改原始帖子并添加XML布局,可能会有所帮助。他指示在创建动态页面时,由于监听器中的ActivateButtons方法,在何处检查按钮是否存在。“在我的教师笔记中”如果这是作业的一部分,请添加作业
标记。我们仍然会帮助你。(这会让我们知道你为什么要这么做。)是的,这是测试按钮是否存在的有效方法。我不知道有家庭作业标签。谢谢你的提醒!谢谢你让我知道这是一种有效的测试方法!