Android后台点击事件

目录

按钮三大步骤

1.根据id获取按钮的点击事件

2.给获取到的事件设置监听

3.处理点击事件

处理点击事件有三种方式

3.1.内部类

3.2匿名内部类

3.3实现onClickListener接口

Android的后台提示

Java单选框和复选框

单选框的点击事件

单选框的动态取值

复选框

获取复选框的选择

获取单选框,复选框,文本框和按钮的结合使用


按钮三大步骤

1.根据id获取按钮的点击事件

        //获取点击事件
        Button btn_paizhao = findViewById(R.id.btn_paizhao);
        Button btn_getphoto = findViewById(R.id.getphoto);
        Button btn_select = findViewById(R.id.select);

2.给获取到的事件设置监听

        //给获取到的事件设置监听
        btn_paizhao.setOnClickListener(this);
        btn_getphoto.setOnClickListener(this);
        btn_select.setOnClickListener(this);

3.处理点击事件

处理点击事件有三种方式

3.1.内部类

    内部类一般用在单个按钮,代码量较多的时候

public class MainActivity5 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        Button btn_text = findViewById(R.id.btn_text);
        btn_text.setOnClickListener(btn_click);

    }
    private View.OnClickListener btn_click=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    };}

3.2匿名内部类

匿名内部类一般用在单个按钮,代码量较少的时候;

public class MainActivity5 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        Button btn_text =findViewById(R.id.btn_text);
        //使用匿名内部类
        btn_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                
            }
        });
    }
}

3.3实现onClickListener接口

实现接口一般用在按钮比较多的情况下

public class MainActivity5 extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        Button btn_text = findViewById(R.id.btn_text);
        btn_text.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
        
    }
}

Android的后台提示

//在控制台输出,在android中依然是可用的,但是不建议使用

System.out.println("hello world");

后台提示,主要是给开发人员使用

// 第一个参数 需要填写字符串 标签
// 第二个参数 需要填写字符串 输出内容
Log.i("登录操作","登录成功");  

前台提示,主要是给用户使用

Toast.makeText(MainActivity.this, "登录登录", Toast.LENGTH_SHORT).show();

Java单选框和复选框

单选框的独立存在: 主要用在同意协议等

单选框一般情况下都是多个出现:比如性别,必须跟RadioGroup结合使用

<RadioGroup
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	orientation="vertical">
	
	<RadioButton
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:checked="true"
		android:text="男"></RadioButton>
	
	<RadioButton
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="女"></RadioButton>
		
</RadioGroup>

单选框的点击事件

和按钮一样也是需要获取点击三大步骤,但是按钮是实现onClickListener接口

但是单选框是实现的onCheckedChangeListener() 的接口;

单选框分为动态取值和静态取值

单选框的动态取值

        //获取控件
        RadioGroup rg_sex = findViewById(R.id.rg_sex);
        rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton rb = findViewById(i);
                String sex = rb.getText().toString();
                Toast.makeText(MainActivity3.this, "您的性别是"+sex, Toast.LENGTH_SHORT).show();
            }
        });

单选框的静态取值

                //单选框的静态取值
                RadioButton rb_man = findViewById(R.id.rb_man);
                RadioButton rb_woman = findViewById(R.id.rb_women);
                String sex;
                if (rb_man.isChecked()){
                    sex=rb_man.getText().toString();
                }else {
                    sex=rb_woman.getText().toString();
                }

复选框

    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="以下哪款手机使用的安卓系统(多选)"/>
    <CheckBox
        android:id="@+id/cb_xiaomi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="小米"/>
    <CheckBox
        android:id="@+id/cb_apple"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="苹果"/>
    <CheckBox
        android:id="@+id/cb_vivo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="vivo"/>
    <CheckBox
        android:id="@+id/cb_oppo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="oppo"/>

获取复选框的选择

        CheckBox cb_xiaomi = findViewById(R.id.cb_xiaomi);
        CheckBox cb_apple = findViewById(R.id.cb_apple);
        CheckBox cb_vivo = findViewById(R.id.cb_vivo);
        CheckBox cb_oppo = findViewById(R.id.cb_oppo);
        cb_xiaomi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    String choie=compoundButton.getText().toString();
                    Toast.makeText(MainActivity3.this, choie, Toast.LENGTH_SHORT).show();
                }
            }
        });

        cb_apple.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    String choie=compoundButton.getText().toString();
                    Toast.makeText(MainActivity3.this, choie, Toast.LENGTH_SHORT).show();
                }
            }
        });
        cb_vivo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    String choie=compoundButton.getText().toString();
                    Toast.makeText(MainActivity3.this, choie, Toast.LENGTH_SHORT).show();
                }
            }
        });
        cb_oppo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    String choie=compoundButton.getText().toString();
                    Toast.makeText(MainActivity3.this, choie, Toast.LENGTH_SHORT).show();
                }
            }
        });

获取单选框,复选框,文本框和按钮的结合使用

public class OperationActivity2 extends AppCompatActivity implements View.OnClickListener {
    Button btn_login;
    CheckBox cb_yes;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operation2);
        //获取控件
        btn_login=findViewById(R.id.btn_login);
        cb_yes=findViewById(R.id.cb_yes);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (cb_yes.isChecked()){
            //如果勾选了同意按钮就获取输入框
            EditText et_name = findViewById(R.id.et_name);
            EditText et_pwd = findViewById(R.id.et_pwd);
            EditText et_email = findViewById(R.id.et_email);
            String name = et_name.getText().toString();
            String pwd = et_pwd.getText().toString();
            String email = et_email.getText().toString();
            Toast.makeText(OperationActivity2.this, "您完成了注册,您的账号是"+name+"您的密码是"+pwd+"您的邮箱是"+email, Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(OperationActivity2.this, "请先勾选同意按钮", Toast.LENGTH_SHORT).show();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android系统中,如果手机受到了后台木马的感染,我们可以采取一些措施来清除它。 首先,我们可以尝试使用安全软件进行扫描和清除后台木马。安全软件会检测系统中的恶意程序,并提供相应的清除操作。使用知名和可信的安全软件可以提高清除木马的效果。 其次,我们可以手动检查应用程序的权限和活动,找出异常和可疑的应用程序。在设置中的应用程序管理下,我们可以查看正在运行的应用程序和最近使用过的应用程序,并清除其中的后台运行程序。特别注意那些没有正当理由和明显作用的应用程序。 另外,我们可以检查系统中的网络连接,排除后台木马的影响。在设置中的网络和连接设置下,我们可以查看正在连接的网络并清除其中的异常连接。如果发现应用程序在后台频繁使用网络或连接到可疑的服务器,应立即进行关闭和断开操作。 最后,如果尝试以上方法仍然无法清除后台木马,建议进行恢复出厂设置。恢复出厂设置将会清除手机中所有的数据和应用程序,包括后台木马。但在进行恢复出厂设置之前,务必备份重要的数据和文件,以免丢失。 总之,清除Android后台木马需要多种方法的组合使用,可以借助安全软件、手动检查应用程序、排除异常网络连接,并在必要时进行恢复出厂设置。同时,平时要增强安全意识,避免点可疑链接和下载不明来源的应用,以保护手机免受后台木马的侵害。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值