您的位置:首页 > 其它

按钮(Button)介绍与应用

2015-01-10 14:38 288 查看
Button在整个组件中的地位非常特殊,其一般情况下扮演"终结者"的角色,例如:用户在一个界面上输入一些信息,很多情况下都有一个"确认"或者"取消"的按钮.

用户的动作一般在这里结束,然后开始另外一系列的动作.

在这个项目的主界面上放置一个按钮,看看它是如何使用的,实现代码如下:

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

   <Button android:id ="@+id/button"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "这个是button" 
   />
</LinearLayout>


代码解释:

上述代码所示,在主界面放了一个android:id为button的按钮,

并设定其宽度(layout_width)和高度(layout_height)都是根据内容调整(wrap_content),

并且设定其显示的文字是"这个是button",

在代码中监听并响应该button的"单击"事件.先看实现代码:

private void find_and_modify_button()
{
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener()
    {
           OnClick(View v)
            {
<pre name="code" class="java">                setTitle("啊哟,button被点了一下");
Intent intent = new Intent(mactivity.this, Bactivity.class);

startActivity(Bactivity);

} });}


代码解释:

上述的监听器实现单击事件代码,对于点击所做的反应是将标题设置为"啊哟,button被点了一下",然后跳转到其他activity.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐