首页 > 其他分享 >3月9号Android开发学习

3月9号Android开发学习

时间:2023-03-11 13:45:52浏览次数:42  
标签:tv Button 学习 开发 result import Android android TextView

按钮控件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>

 

标签:tv,Button,学习,开发,result,import,Android,android,TextView
From: https://www.cnblogs.com/cinan/p/17205578.html

相关文章