3月8号Android开发学习

按钮控件Button

按钮控件Button由TextView派生而来,他们之间的区别有:

(1)Button拥有默认的按钮背景,而TextView默认无背景

(2)Button内部文本默认居中对齐,而TextView的内部文本默认靠左对齐

(3)Button会默认将英文字母转为大写,而TextView保持原始的英文大小写

按钮控件的新增属性

(1)textAllCaps属性,它指定了是否讲英文字母转为大写,为true是表示自动转为大写,为false表示不做大写转换。

(2)onClick属性,它用来接管用户的点击动作,指定了点击按钮时要触发那个方法(过时了)

复制代码
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.util.DateUtil;

public class MainActivity3 extends AppCompatActivity {

    private TextView tv_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        tv_button = findViewById(R.id.tv_button);
    }
    public void doClick(View view)
    {
        String desc =String.format("%s", DateUtil.getNowTime());
        tv_button.setText(desc);
    }
}
复制代码
复制代码
<?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"
    android:padding="5dp">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello world"
        android:textColor="#000000"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="hello world"
        android:textColor="#000000"
        ></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="直接指定点击方法"
        android:textColor="#000000"
        android:onClick="doClick"
        ></Button>
    <TextView
        android:id="@+id/tv_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#DA3636"
        android:textSize="17sp"
        ></TextView>
</LinearLayout>
复制代码

点击按钮时间的监听

复制代码
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.util.DateUtil;

public class MainActivity4 extends AppCompatActivity {

    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        tv_result = findViewById(R.id.tv_result);
        Button but_click=findViewById(R.id.but_click);
        but_click.setOnClickListener(new MyOnClickListener(tv_result));
    }
    //防止内存泄漏,
    static class MyOnClickListener implements View.OnClickListener{

        private final TextView tv_result;
        //创造一个构造函数
        public MyOnClickListener(TextView tv_result) {
            this.tv_result=tv_result;
        }

        @Override
        public void onClick(View view) {
            String desc =String.format("%s", DateUtil.getNowTime());
            tv_result.setText(desc);
        }
    }
}
复制代码
复制代码
<?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:id="@+id/but_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="指定单独的点击监听器"
        android:textColor="#000000"
        android:textSize="15sp"
        ></Button>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:text="查看点击结果"
        android:textColor="#000000"
        android:textSize="15sp"></TextView>

</LinearLayout>
复制代码

按钮长按监听器

复制代码
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.util.DateUtil;

public class ButtonLongClickActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_long_click);
        Button but_long_click=findViewById(R.id.but_long_click);
        TextView tv_result=findViewById(R.id.tv_result);
        but_long_click.setOnLongClickListener(view -> {
            String desc =String.format("%s", DateUtil.getNowTime());
            tv_result.setText(desc);
            return true;
        });
        
    }
}
复制代码
复制代码
<?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:id="@+id/but_long_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="指定单独的长按监听器"
        android:textColor="#000000"
        android:textSize="15sp"
        ></Button>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:text="查看点击结果"
        android:textColor="#000000"
        android:textSize="15sp"></TextView>

</LinearLayout>
复制代码

 

posted @   辞楠  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示