使用View类而不是Button类在Android中创建命令按钮?

时间:2012-03-12 13:12:55

标签: android button view

我正在学习Android编程,我在活动类(而不是xml)中创建命令按钮的方式是"import android.widget.Button;",然后创建Button实例,例如"Button exit = (Button) findViewById(R.id.exitButton);"

但是我遇到了一些源代码文件,其中包含一个用作按钮的View实例。其余代码似乎相似,但唯一的区别是他使用的是View类而不是Button类。

有人可以告诉我这两种方法之间的区别吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

以下代码均有效:

  1. Button exit = (Button) findViewById(R.id.exitButton);
  2. View exit = (Button) findViewById(R.id.exitButton);
  3. 因为查看是所有小部件的 super class ,例如Button,ListView,GridView,RelativeLayout,LinearLayout等。

    请查看此按钮以获取按钮类:

    enter image description here

    现在,请检查以下快照中的 RelativeLayout

    enter image description here