Day3 按钮的相关运用

1.实现点击过后按钮里文本的改变和按钮的不可点击

1.xml配置文件
    >
<Button
    android:id="@+id/bt_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="登录"
    android:textColor="#ff0000"
    android:textSize="25sp"


    android:background="#0000ff"
    android:tag="1"
        />
    <Button
        android:id="@+id/bt_done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="完成"
        android:layout_below="@+id/bt_login"
        android:layout_marginTop="20dp"
        android:enabled="false"
        />
    <Button
        android:id="@+id/bt_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="编辑"
        android:layout_alignTop="@+id/bt_done"
        android:layout_alignRight="@+id/bt_login"

        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示的文本"
        android:textColor="@drawable/text_color"

        />
2.实现具体按钮操作
public class MainActivity extends AppCompatActivity implements  View.OnClickListener{
//boolean isnormal=true;
    Button done;
    Button edit;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

          done=findViewById(R.id.bt_done);
           edit=findViewById(R.id.bt_edit);
           btn=findViewById(R.id.bt_login);

        //给按钮添加点击事件
        btn.setOnClickListener(this);
        done.setOnClickListener(this);
        edit.setOnClickListener(this);

       done.setOnClickListener(view->{
           edit.setEnabled(true);
           done.setEnabled(false);
       });
       edit.setOnClickListener(view->{
           edit.setEnabled(false);
           done.setEnabled(true);
       });

    }
 public void onClick(View view) {
        System.out.println("点击了");
        //使用的时候转化为对应的子类类型
        Button btn=(Button)view;
    if(btn.getId()==R.id.bt_login){
            if(btn.getTag().equals("1")){
                btn.setText("退出");
                btn.setTag("0");
            }else {
                btn.setText("登录");
                btn.setTag("1" );
            }
        }

      }
    }

3.注意

按钮的点击事件 通常接受一个参数:view
当按钮被点击 系统接受这个事件
并把这个事件回调给监听者
把当前被点击的按钮作为参数传递过去
Button ImageView
注意:使用的时候 必须转化为对应的类型
不建议使用

2.实现按钮形状的颜色形状的设计

1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"

    android:orientation="vertical">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="分享"
    android:enabled="true"
    android:textColor="@drawable/text_color"
    />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示的文本"
        android:textColor="@drawable/color"
        />
    <Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:enabled="true"
        android:background="@drawable/shape_rectangle"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttonshapeandcolor"
        android:enabled="true"

        />

</LinearLayout>
image.png

corners:圆角的大小
solid:填充颜色
stroke:边框颜色 宽度
gradient;渐变色 UI-拟物化-扁平化设计
padding:内间距

2.shape模块
<shape xmlns:android="http://schemas.android.com/apk/res/android">

android:shape="rectange">
    <stroke android:color="@color/colorAccent"
        android:width="1dp"/>

<corners
    android:radius="15dp"/>
<solid
android:color="@color/colorAccent"
        />
    <gradient android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:gradientRadius="50dp"
        android:type="sweep"
        />
<padding
    />

</shape>
3.text模块
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--配置是有先后顺序的
    默认的状态必须放在后面
  如果放在前面,优先匹配一旦匹配上了 后面的设置就无效了
  -->
    <!--按下状态的颜色 pressed-->
<item android:color="#ff0000"
    android:state_pressed="true"/>

    <!--无效 无法点击的颜色enable-->
    <item android:color="#00ff00"
        android:state_enabled="false"/>
    <!--默认状态-->
    <item android:color="#000000" />


</selector>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,551评论 0 17
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,543评论 1 11
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,145评论 1 32
  • 1、人与人的关系,沉默是疏远的开始。 2、你一直期待的东西也许不适合拥有,更适合放在心里珍惜。 3、没有人不辛苦,...
    禾利阅读 1,591评论 13 89
  • 在大理 (1) 4000米的苍山,矗立在你身边守望 你用你的蓝把灰黄的我漂染 你凝望天空,与银河对峙 与日月同辉 ...
    李鱼2019阅读 162评论 0 1