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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构