• Android常使用的控件(上)


    TextView控件

      该控件一般是展示一些文本提示内容,和HTML中的 lable标签相似(纯属个人意见)。现在来用代码描述一下TextView的使用。

      设置TextView的使用有多种方式(常用的两种):

      第一:界面android xml文件直接进行赋值

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    
    <TextView
    android:id="@+id/txtOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
    <TextView
    android:id="@+id/txtTwo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello_world 直接显示" />
    </RelativeLayout>

     代码中的灰色代码就是赋值的方式,txtOne是利用资源文件夹中的res—values—strings.xml中来赋值

    strings.xml代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Demo1</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
    
    </resources>

    这样就可以现在手机屏幕显示出 hello world、hello world 直接显示 的字符串了。

    第二:java代码后台实现

        

        
        TextView txtOne,txtTwo;
        @Override
        protected void onCreate(Bundle savedInstanceState) {//每个Activity创建的时候的入口函数
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            txtOne=(TextView)this.findViewById(R.id.txtOne);  //根据id获取到控件
            txtTwo=(TextView)this.findViewById(R.id.txtTwo);
            txtOne.setText("这个是给txtOne赋值");
            txtOne.setText("这个是给txtTwo赋值");
        }

      这样运行后,手机上就会显示  这个是给txtOne赋值,这个是给txtTwo赋值  两个字符串

      Button控件

      这个控件听名字大家都会觉得很熟悉吧!他运用范围很宽广,下面要介绍一下他的简单的运用

      一:监听事件

          上面的TextView中介绍了对控件的显示值的控制,Button和TextView的显示值和它一样,下面我直接进行事件的监听演示

          界面的xml代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
        <Button 
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/btnDemo"
         android:text="按钮一"
            />
    </LinearLayout>

          java端代码:

        Button btnDemo;
        @Override
        protected void onCreate(Bundle savedInstanceState) {//每个Activity创建的时候的入口函数
            super.onCreate(savedInstanceState);
            btnDemo=(Button)this.findViewById(R.id.btnDemo);
            btnDemo.setOnClickListener(new OnClickListener()); //添加按钮的监听事件,这里是实例化一个实现接口OnClickListener的类
        }
        private class OnClickListener implements android.view.View.OnClickListener{
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(arg0==btnDemo){
                    Toast.makeText(MainActivity.this, "你点击了按钮", Toast.LENGTH_LONG).show(); //这个一个对话框,用的范围也比较多  
                    //参数1:Context,参数2:弹出框的显示文本 参数3:显示的形式
                }
            }
            
        }
  • 相关阅读:
    介绍我的一位同事的开源RSS阅读器
    开源协议概谈[转载]
    编译错误CS1595
    JAVA和C#,武当和少林之争!
    IBatisNet之获取和操作SQL语句
    Linux能否靠架构取得胜利
    开源CMS Alfresco 1.0 发布
    在asp.net页面上得到Castle容器的实例
    IBatisNet 之 自动生成主关键字
    onvaluechange事件
  • 原文地址:https://www.cnblogs.com/cindyOne/p/2988641.html
Copyright © 2020-2023  润新知