欢迎光临散文网 会员登陆 & 注册

第五章 用户界面基础(Button)

2018-11-03 21:47 作者:swiss126  | 我要投稿

参考资料:

Android应用程序开发》ISBN 9787302283164

参考软件:

Android Studio、Eclipse+ADT、Android SDK、JDK

用户界面基础(接上节内容)

三、Button

常见属性

   setAutoLinkMask(int)控制链接网址和电子邮件地址等是否自动发现并转换为可点击的链接。

 setKeyListener(KeyListener)如果设置,指定该TextView文本输入法和自动纠正一些常见的拼写错误

 setText(CharSequence TextView.BufferType)确定最低类型getText()将返回。

 setKeyListener(KeyListener)如果设置,指定该TextView文本输入法,应该自动利用用户类型。

 setCursorVisible(布尔)使得光标(默认)可见或不可见。

android:数字setKeyListener(KeyListener)如果设置,指定该TextView数字输入法,这些特定的字符,它将接受的。

 setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)可拉的得出以下文本。

 setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)绘制的是左边的文本。

 setCompoundDrawablePadding(int)之间的填充画板和文本。

 setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)绘制的是右边的文本。

 setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)上面的得出可拉的文本。

android:可编辑如果设置,指定该TextView输入法。

 setInputExtras(int)引用一个< input-extras > XML资源包含额外的数据提供一个输入方法,这是私人的实现输入法。

 setEllipsize(TextUtils.TruncateAt)如果设置,导致单词以上的观点是碎ellipsized宽,而不是在中间。

 setEms(int)使TextView正是这许多ems宽。

 setFreezesText(布尔)如果设置文本视图将包含当前完成文本内的冷冻冰柱除了元数据,比如当前光标位置。

android:重力setGravity(int)指定如何对齐文本视图的x和y轴小于文本时的观点。android:高度setHeight(int)使TextView正是这许多像素高。

android:提示setHint(int)时所显示的提示文本的文本是空的。

android:imeActionId setImeActionLabel(CharSequence int)为EditorInfo提供一个值。 actionId时使用一个输入连接到文本视图方法。

android:imeActionLabel setImeActionLabel(CharSequence int)为EditorInfo提供一个值。 actionLabel时使用一个输入连接到文本视图方法。

android:imeOptions setImeOptions(int)附加功能您可以启用一个输入法与一个编辑器来提高与应用程序的集成。

android:includeFontPadding setIncludeFontPadding(布尔)留下足够的字体就是和流畅的空间而不是使用严格字体上升和下降。

android:inputMethod setKeyListener(KeyListener)如果设置,指定该TextView应该使用指定的输入方法(指定完全限定类名)。

android:inputType setRawInputType(int)被放置在一个文本字段的数据类型,用来帮助一个输入法决定如何让用户输入文本。

android:lineSpacingExtra setLineSpacing(浮子,浮子)额外间距行文本。

android:lineSpacingMultiplier setLineSpacing(浮子,浮子)额外间距行文本,乘数效应。

android:行setLines(int)使TextView正是这许多行高。

android:linksClickable setLinksClickable(布尔)如果设置为false,阻碍运动方法将运动的联系方法即使autoLink原因被发现的链接。

android:marqueeRepeatLimit setMarqueeRepeatLimit(int)字幕动画重复的次数。

android:maxEms setMaxEms(int)使TextView最多这许多ems宽。

android:maxHeight setMaxHeight(int)使TextView最多这许多像素高。

android:最大长度setFilters(InputFilter)设置一个过滤器来限制输入的文本长度到指定的号码。

android:maxLines setMaxLines最多(int)使TextView被这麽多行高。

android:maxWidth setMaxWidth最多(int)使TextView被这麽多像素宽。

android:minEms setMinEms(int)使TextView至少这许多ems宽。

android:minHeight setMinHeight(int)使TextView至少这许多像素高。

android:minLines setMinLines(int)使TextView至少这许多行高。

android:minWidth setMinWidth(int)使TextView至少这许多像素宽。

android:数字setKeyListener(KeyListener)如果设置,指定该TextView数字输入法。

android:密码setTransformationMethod(TransformationMethod)字段的字符是否显示为密码点,而不是自己。

android:phoneNumber setKeyListener(KeyListener)如果设置,指定该TextView有电话号码的输入方法。

android:privateImeOptions setPrivateImeOptions(String)添加内容类型描述提供输入方法在文本视图,这是私人的实现输入法。

android:scrollHorizontally setHorizontallyScrolling(布尔)文本是否允许更广泛的比视图(因此可以水平滚动)。

android:selectAllOnFocus setSelectAllOnFocus(布尔)如果文本是可选择的,选择它当视图所需要的关注,而不是将光标移动到开始或结束。

android:shadowColor setShadowLayer(浮子,浮子,浮子,int)放置一个指定颜色的阴影背后的文本。

android:shadowDx setShadowLayer(浮动,浮动,浮动,int)水平偏移的影子。

android:shadowDy setShadowLayer(浮子,浮子,浮子,int)垂直偏移量的影子。

android:shadowRadius setShadowLayer(浮子,浮子,浮子,int)半径的影子。

android:单行模式setTransformationMethod(TransformationMethod)限制了文本一行水平滚动,而不是让它包装到多个行,并提出重点而不是按enter键时插入一个新行。

android:文本setText(CharSequence TextView.BufferType)文本显示。

android:textAppearance基本文本颜色,字体,大小和风格。

android:textColor setTextColor(int)文本颜色。

android:textColorHighlight setHighlightColor(int)的颜色选择突出显示的文本。

android:textColorHint setHintTextColor(int)提示文本的颜色。

android:textColorLink setLinkTextColor(int)链接的文本颜色。

android:textIsSelectable isTextSelectable()表明的内容不可编辑的文本可以选择。

android:textScaleX setTextScaleX(浮动)设置文本的水平扩展因素。

android:textSize setTextSize(int,浮动)大小的文本。

android:textStyle setTypeface(字体)风格(粗体、斜体、bolditalic)文本。

android:字体setTypeface(字体)字体(正常、无衬线、等宽字体)的文本。

android:宽度setWidth(int)使TextView正是这许多像素宽。

常用代码

1、Android按钮单击事件的四种常用写法

很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同。因此本文就把这些写法总结一下,比较下各种写法的优劣,希望对大家灵活地选择编码方式可以有一定的参考借鉴价值。

xml文件代码如下:

<Button

    android:id="@+id/button1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Button1" />

  <Button

    android:id="@+id/button2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Button2" />

四种方法分述如下:

匿名内部类:

public class TestButtonActivity extends Activity {

  Button btn1, btn2;

  Toast tst;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test_button);

    btn1 = (Button) findViewById(R.id.button1);

    btn2 = (Button) findViewById(R.id.button2);

    btn1.setOnClickListener(new OnClickListener() {

      @Override

      public void onClick(View v) {

        // TODO Auto-generated method stub

        Toast tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT);

        tst.show();

      }

    });

    btn2.setOnClickListener(new OnClickListener() {

      @Override

      public void onClick(View v) {

        // TODO Auto-generated method stub

        Toast tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT);

        tst.show();

      }

    });

  }

}

自定义单击事件监听类:

public class TestButtonActivity extends Activity {

  Button btn1, btn2;

  Toast tst;

  class MyClickListener implements OnClickListener {

    @Override

    public void onClick(View v) {

      // TODO Auto-generated method stub

      switch (v.getId()) {

      case R.id.button1:

        tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT);

        tst.show();

        break;

      case R.id.button2:

        tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT);

        tst.show();

        break;

      default:

        break;

      }

    }

  }

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test_button);

    btn1 = (Button) findViewById(R.id.button1);

    btn2 = (Button) findViewById(R.id.button2);

    btn1.setOnClickListener(new MyClickListener());

    btn2.setOnClickListener(new MyClickListener());

  }

}

Activity继承View.OnClickListener,由Activity实现OnClick(View view)方法,在OnClick(View view)方法中用switch-case对不同id代表的button进行相应的处理

public class TestButtonActivity extends Activity implements OnClickListener {

  Button btn1, btn2;

  Toast tst;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test_button);

    btn1 = (Button) findViewById(R.id.button1);

    btn2 = (Button) findViewById(R.id.button2);

    btn1.setOnClickListener(this);

    btn2.setOnClickListener(this);

  }

  @Override

  public void onClick(View v) {

    // TODO Auto-generated method stub

    switch (v.getId()) {

    case R.id.button1:

      tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT);

      tst.show();

      break;

    case R.id.button2:

      tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT);

      tst.show();

      break;

    default:

      break;

    }

  }

}

最后一种是我今天看到的一种写法,在XML文件中“显示指定按钮的onClick属性,这样点击按钮时会利用反射的方式调用对应Activity中的click()方法”

<Button

    android:id="@+id/button1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="onClick"

    android:text="Button1" />

  <Button

    android:id="@+id/button2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="onClick"

    android:text="Button2" />

这里在输完android:的时候按下 Alt+/ 会有 onClick 属性的提示, 但输入到 android:onClick=“ 的地方按下 Alt+/ 并没有提示 onClick 选项,让我突然觉得这里好像有点问题。

public class TestButtonActivity extends Activity {

  Button btn1, btn2;

  Toast tst;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test_button);

  }

  // 注意 这里没有 @Override 标签

  public void onClick(View v) {

    // TODO Auto-generated method stub

    switch (v.getId()) {

    case R.id.button1:

      tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT);

      tst.show();

      break;

    case R.id.button2:

      tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT);

      tst.show();

      break;

    default:

      break;

    }

  }

}

这种写法整个代码中都不用声明button就可以实现button的单击事件。

以上就是四种实现按钮单击事件的方法。

粗略总结一下,就是按钮少的时候用匿名内部类会比较快,比如写demo测试的时候或者登陆界面之类的。

按钮多的情况我还是选择第三种方法,方便。

关于第四种方法,我感觉最方便,但看了很多代码还是觉得写法不够大众化,感兴趣的朋友可以对此研究研究。相信会有不少收获。

2、Android自定义Button按钮显示样式

现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的。今天学习自定义Button按钮样式。Button样式修改的是Button的背景(Background)属性。
首先写一个定义Button样式的XML文件:
新建Android XML文件,类型选Drawable,根结点选selector,文件名就buton_style吧。
程序自动给我们刚刚建的文件里加了selector结点,我们只需要在selector结点里写上三种状态时显示的背景图片(按下、获取焦点,正常)。
代码如下:
Xml代码 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/play_press" /> 
    <item android:state_focused="true" android:drawable="@drawable/play_press" /> 
    <item android:drawable="@drawable/play" /> 
</selector>  
 
我这里获取焦点跟点击时显示的是同一张图片,必须严格照上面的顺序写,不可倒。
接下来只要在布局时写Button控件时应用到Button的Background属性即可。
Xml代码 
<Button android:id="@+id/button1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:background="@drawable/button_style" 
></Button>  
 
最终效果图:
再加上一种自定义样式方法,上面的是用图片,其实我们可以直接通过定义xml文件来实现不同的样式:
在上面的源代码基础上,只需要修改button_style文件,同样三种状态分开定义:
Xml代码 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
        <shape> 
            <gradient android:startColor="#0d76e1" android:endColor="#0d76e1" 
                android:angle="270" /> 
            <stroke android:width="1dip" android:color="#f403c9" /> 
            <corners android:radius="2dp" /> 
            <padding android:left="10dp" android:top="10dp" 
                android:right="10dp" android:bottom="10dp" /> 
        </shape> 
    </item> 
  
    <item android:state_focused="true"> 
        <shape> 
            <gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7" 
                android:angle="270" /> 
            <stroke android:width="1dip" android:color="#f403c9" /> 
            <corners android:radius="2dp" /> 
            <padding android:left="10dp" android:top="10dp" 
                android:right="10dp" android:bottom="10dp" /> 
        </shape> 
    </item> 
  
    <item> 
        <shape> 
            <gradient android:startColor="#000000" android:endColor="#ffffff" 
                android:angle="180" /> 
            <stroke android:width="1dip" android:color="#f403c9" /> 
            <corners android:radius="5dip" /> 
            <padding android:left="10dp" android:top="10dp" 
                android:right="10dp" android:bottom="10dp" /> 
        </shape> 
    </item> 
</selector>  
 
gradient 主体渐变 startColor开始颜色,endColor结束颜色 ,angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)
stroke 边框 width 边框宽度,color 边框颜色
corners 圆角 radius 半径,0为直角
padding text值的相对位置

3、圆角的Button

在res目录下的drawable-mdpi建立xml文件shape.xml,如下图所示:

shape.xml

<?xml version="1.0"encoding="UTF-8"?>

<shape

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <!-- 填充的颜色 -->

    <solid android:color="#FFFFFF"/>

    <!-- 设置按钮的四个角为弧形 -->

    <!-- android:radius 弧形的半径 -->

    <corners android:radius="5dip"/>

     

<!-- padding:Button里面的文字与Button边界的间隔 -->

<padding

   android:left="10dp"

   android:top="10dp"

   android:right="10dp"

   android:bottom="10dp"

/>

</shape>

 

main.xml

在android:background="@drawable/shape"就使用了shape.xml资源

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView   

    android:layout_width="fill_parent"  

    android:layout_height="wrap_content"  

    android:text="@string/hello"

    />  

<Button  

    android:id="@+id/roundButton"

    android:text=" 圆角按钮 "

    android:layout_width="wrap_content"  

    android:layout_height="wrap_content"  

    android:background="@drawable/shape"

    />

</LinearLayout>

strings.xml

<?xml version="1.0"encoding="utf-8"?>

<resources>

    <string name="hello">Hello World, RoundButtonDemoActivity!</string>

    <string name="app_name">RoundButtonDemo</string>

</resources>

RoundButtonDemoActivity.java

package com.android.RoundButtonDemo.activity; 

 

import android.app.Activity; 

import android.os.Bundle; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.Toast; 

 

publicclass RoundButtonDemoActivity extends Activity { 

    Button roundButton; 

    @Override

    publicvoid onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

         

       roundButton=(Button)findViewById(R.id.roundButton); 

      //使用匿名类注册Button事件 

        roundButton.setOnClickListener(new OnClickListener() 

      {      

            publicvoid onClick(View v) 

            { 

               Toast.makeText(RoundButtonDemoActivity.this,"你点击了圆角按钮",Toast.LENGTH_LONG).show(); 

            } 

        }); 

    } 

}

效果图:

4、动画按钮

http://blog.csdn.net/qiujuer/article/details/39831451


第五章 用户界面基础(Button)的评论 (共 条)

分享到微博请遵守国家法律