美文网首页
安卓基础控件使用(TextView、Button、ImageVi

安卓基础控件使用(TextView、Button、ImageVi

作者: mank | 来源:发表于2018-03-16 08:04 被阅读0次

    一、文本控件TextView
    1.布局文件

    <TextViewandroid:id="@+id/tv_show"
    android:text="@string/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/green"
    android:textSize="@dimen/title"
    android:lines="1"
    android:maxWidth="40dp"
    android:ellipsize="middle"
    android:focusable="true"
    android:focusableInTouchMode="true"/>
    

    2.控件属性

    android:id  控件唯一标识
    android:text    显示的文本信息
    android:layout_width    控件宽度
    android:layout_height   控件高度
    android:textSize    字体大小
    android:textColor   字体颜色
    android:lines   文本显示行数
     android:maxWidth   最大显示宽度
    android:ellipsize   设置当文本过长时如何显示文本内容
    start:省略号显示在开头
    middle:省略号显示在中间
    end:省略号显示在结尾
    marquee:以跑马灯方式显示
     
    android:focusable   是否获得焦点
    android:
    focusableInTouchMode    触摸模式后是否可获得焦点
    

    3.对象获取

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //获取文本对象
        TextView tv_show = (TextView) findViewById(R.id.tv_show);
        //获取android:text属性值
        String text = tv_show.getText().toString();
        //后台日志输出
        Log.i("wl",text);
        //设置android:text
        tv_show.setText("Hello Man");
    
        //通过getResources()获得资源常量    tv_show.setTextColor(getResources().getColor(R.color.colorPrimary));
        //吐司 在app中输出
        Toast.makeText(this,text,Toast.LENGTH_LONG).show();
    
    }
    

    二、按钮控件Button
    1.布局文件

    <Buttonandroid:id="@+id/btn_show"
    android:text="按钮"
    android:textSize="20sp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="btnClick"/>
    

    2.注册监听

    (1)匿名内部类
    //获取按钮对象
    Button btn_show = (Button) findViewById(R.id.btn_show); 
    //注册点击监听 
    btn_show.setOnClickListener(new View.OnClickListener() { 
      @Override public void onClick(View v) { 
      Toast.makeText(MainActivity.this,"点击按钮",Toast.LENGTH_LONG).show(); 
      } 
    }); 
    
    (2)接口实现
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
       //声明控件对象    Button btn_show ;
       
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           
           //获取按钮对象
           btn_show = (Button) findViewById(R.id.btn_show);
           //注册点击监听
           btn_show.setOnClickListener(this);
    
       }
       //实现接口类    @Override
       public void onClick(View v) {
           Toast.makeText(this,"点击按钮",Toast.LENGTH_LONG).show();
       }
    }
    
    (3)设置onclick属性
    public void btnClick(View v){
       Toast.makeText(this,"点击按钮",Toast.LENGTH_LONG).show();
    }
    

    3.按钮背景图片设置及点击效果

    (1)在res/drawable下创建btn_selector.xml,选择选中和没选中时的背景图片
    <?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:drawable="@drawable/btn_bg2" android:state_pressed="false"/>
       <item android:drawable="@drawable/btn_bg_p" android:state_pressed="true"/></selector>
    
    (2)按钮布局文件中背景图片使用btn_selector.xml
    <Buttonandroid:text="卸载"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:background="@drawable/btn_selector"
    android:textColor="#fff"
    android:textSize="18sp"/>
    

    三、图片控件ImageView
    1.布局文件

    <ImageViewandroid:src="@drawable/danger"
    android:background="@drawable/danger"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    

    2.控件属性
    android:src
    设置ImageView中显示的图片
    – 是前景,显示在前面
    – 可根据宽高缩放,但是保持图片原有比例
    android:background
    设置ImageView控件的背景
    – 是背景,显示在后面
    – 可根据宽高缩放,但是不保持图片原有比例
    – 除了图片以外,背景还可以是颜色

    3.图片资源
    (1)注意命名中不得含有中文或大写字母
    (2)首字母必须以字母开头
    (3)格式png,jpg

    四、输入控件EditText
    1.布局文件

    <EditTextandroid:hint="请输入"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    

    用户注册界面开发

    任务目标:
    1.掌握RelativeLayout布局的使用方法
    2.掌握RadioButton和RadioGroup控件的使用方法
    3.掌握CheckBox控件的使用方法
    任务陈述:
    使用RelativeLayout布局管理器和CheckBox控件及RadioGroup控件实现用户注册界面开发。该界面用于输入用户的注册信息,包括用户名,邮箱,密码及确认密码的输入,并进行性别的选择,进行是否显示密码和同意协议的多选,最后点击注册,实现注册功能。
    任务学习:
    5.1 RelativeLayout布局的使用
    在Android中,相对布局是指按照组件之间的相对位置进行布局,这种方式允许子元素指定它们相对于其他元素或父元素的位置(通过ID指定)。
    在开发中,可以在XML布局文件定义相对布局管理器,其基本语法如下:


    image.png
    RelativeLayout常见的XML属性列表

    5.2掌握RadioButton和RadioGroup控件的使用方法
    在Android中,单选按钮(RadioButton)继承了普通按钮。因此,它们都可以直接使用普通按钮支持的的各种属性和方法。
    单选按钮(RadioButton),可以通过在XML布局文件中使用<RadioButton>标记添加,其基本语法格式如下:


    image.png
    RadioButton常见的XML属性列表
    image.png
    若要RadioButton组件与RadioGroup组件一起使用时,即构成一个单选按钮组。在XML布局文件中,添加RadioGroup组件的基本语法格式如下:
    image.png
    5.3 CheckBox控件的使用
    在Android中,复选框用CheckBox表示,,而CheckBox类是Button类的子类,所以可以直接使用Button支持的各种属性。可以通过在XML布局文件中使用<CheckBox>标记添加,其语法格式如下:
    image.png
    在Eclipse下创建一个Android项目,命名为AndroidDemo2.5,实现用户注册界面开发。
    1.修改res/layout目录下的布局文件,首先添加一个相对布局管理器,在该布局管理器中嵌套添加一个线性布局管理器,并在其在内部添加四个线性布局管理器,在第一个线性布局管理器中,添加一个TextView控件和一个EditText控件,实现用户名的输入。
    2.在第二个线性布局管理器中,继续添加一个TextView控件和一个EditText控件,用于密码的输入,并添加一个CheckBox决定是否显示密码。
    3.在第三个线性布局管理器中,继续添加一个TextView控件和一个EditText控件,用于确认密码的输入。
    4.在第四个线性布局管理器中,继续添加一个RadioGroup控件,在RadioGroup控件中,添加两个RadioButton按钮,用于性别的选择。

    5.在外层的线性布局管理器中,添加一个Button按钮,点击按钮,实现注册功能。
    6.界面设计呈现的效果如下图所示


    image.png

    相关文章

      网友评论

          本文标题:安卓基础控件使用(TextView、Button、ImageVi

          本文链接:https://www.haomeiwen.com/subject/lddqqftx.html