学习安卓开发遇到的问题

问题1:学习禁用与恢复按钮中:

java代码报错:报错代码是

R.id.btn_enable;case R.id.btn_disable;case R.id.btn_test:

代码如下:(实现功能在代码后面)

package com.example.apptest;

import static java.time.LocalTime.now;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_result;
    private Button btn_test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_result = findViewById(R.id.tv_result);
        btn_test = findViewById(R.id.btn_test);

        Button btn_enable = findViewById(R.id.btn_enable);
        Button btn_disable = findViewById(R.id.btn_disable);
        btn_enable.setOnClickListener(this);
        btn_disable.setOnClickListener(this);
        btn_test.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_enable:
                    btn_test.setTextColor(Color.BLACK);
                    btn_test.setEnabled(true);
                    break;
                case R.id.btn_disable:
                    btn_test.setTextColor(Color.GRAY);
                    btn_test.setEnabled(false);
                    break;
                case R.id.btn_test:
                    tv_result.setText(now().toString() + "  测试按钮被点击了");
                    break;
            }
        }
    }
}


<?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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_enable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="启动测试按钮"
            android:textColor="#000000"
            android:layout_weight="1"
            android:textSize="15sp"
            />
        <Button
            android:id="@+id/btn_disable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="禁用测试按钮"
            android:textColor="#000000"
            android:layout_weight="1"
            android:textSize="15sp"
            />

    </LinearLayout>
    <Button
        android:id="@+id/btn_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="测试按钮"
        android:textColor="#888888"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:text="这里查看测试按钮的点击结果"
        android:textColor="#000000"
        android:textSize="15sp"/>

</LinearLayout>

 某些情况希望暂时禁止点击操作,譬如用户在注册的时候,有 的网站要求用户必须同意指定条款,而且至少浏览10秒之后才能点击注册按钮。那么在10秒之前,注册 按钮应当置灰且不能点击,等过了10秒之后,注册按钮才恢复正常。在这样的业务场景中,按钮先后拥 有两种状态,即不可用状态与可用状态,它们在外观和功能上的区别如下:

(1)不可用按钮:按钮不允许点击,即使点击也没反应,同时按钮文字为灰色。

(2)可用按钮:按钮允许点击,点击按钮会触发点击事件,同时按钮文字为正常的黑色。

注:原因在评论区,有人提出来了,开心!!感谢!

问题2:4.2 在活动之间传递消息中:

向下一个Activity发送数据,并且下一个向上一个Activity返回响应数据学习中遇到问题:

上一个Activity没有接收到下一个的响应数据:

点击发送以上文字按钮后: 

点击返回应答消息按钮后: 

收到的返回信息为空! 

代码如下:

MainActivity.java

package com.example.apptest;

import static java.time.LocalTime.now;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

    }

    @Override
    public void onClick(View v) {
            if (v.getId() == R.id.btn_enable){
                TextView tv_send = findViewById(R.id.tv_send);
                //创建一个意图对象,准备跳到指定的活动页面
                Intent intent = new Intent(this,Activity2.class);
                //创建一个Bundle对象(创建一个新包裹),准备存储数据
                Bundle bundle = new Bundle();
                // 往包裹存入名为request_time的字符串
                bundle.putString("request_time",now().toString());
                // 往包裹存入名为request_content的字符串
                bundle.putString("request_content",tv_send.getText().toString());
                // 把快递包裹塞给意图
                intent.putExtras(bundle);
                //期望接收下个页面的返回数据。第二个参数为本次请求代码
                startActivityForResult(intent,0);
            }
    }


    // 从下一个页面携带参数返回当前页面时触发。其中requestCode为请求代码,
// resultCode为结果代码,intent为下一个页面返回的意图对象

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent intent){
        // 接收返回数据
        super.onActivityResult(requestCode,resultCode,intent);

        TextView tv_response = findViewById(R.id.tv_response);
        // 意图非空,且请求代码为之前传的0,结果代码也为成功
        if (intent!=null && requestCode == 0 && resultCode == MainActivity.RESULT_OK){
            // 从返回的意图中获取快递包裹
            Bundle bundle = intent.getExtras();
            // 从包裹中取出名叫response_time的字符串
            String response_time = bundle.getString("responde_time");
            // 从包裹中取出名叫response_content的字符串
            String response_content = bundle.getString("responde_content");

            String desc = String.format("收到返回消息:\n应答时间为:%s\n应答内容为:%s",response_time,response_content);
            tv_response.setText(desc); // 把返回消息的详情显示在文本视图上
        }
    }
}


Activity2.java

package com.example.apptest;

import static java.time.LocalTime.now;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Activity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        TextView tv_receive = findViewById(R.id.tv_receive);
        Button btn_finish = findViewById(R.id.btn_finish);
        // 从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getExtras();
        // 从包裹中取出名为request_time的字符串
        String request_time = bundle.getString("request_time");
        // 从包裹中取出名为request_content的字符串
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s",request_time,request_content);
        tv_receive.setText(desc);

/*
下一个页面在返回上一个页面时,打包应答数据并调用setResult方法返回数据包裹。setResult
方法的第一个参数表示应答代码(成功还是失败),第二个参数为携带包裹的意图对象。
*/

        btn_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String response = "我吃过了,还是你来我家吃";
                Intent intent = new Intent();
                Bundle bundle2 = new Bundle();
                // 往包裹存入名为字符串
                bundle2.putString("response_time", now().toString());
                bundle2.putString("response_content", response);
                // 将包裹存入意图中
                intent.putExtras(bundle2);
                // 携带意图返回上一个页面。RESULT_OK表示处理成功
                setResult(Activity.RESULT_OK,intent);
                finish();// 结束当前的活动页面
            }
        });

    }
}

 activity_main.xml

<?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">

    <TextView
        android:id="@+id/tv_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:text="哈哈,今天天气真好!"
        android:textColor="#000000"
        android:textSize="15sp"/>


    <Button
            android:id="@+id/btn_enable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="发送以上文字"
            android:textColor="#000000"
            android:textSize="15sp"
            />

    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:textColor="#000000"
        android:textSize="15sp"/>






</LinearLayout>

 activity_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_receive"
        android:text="我接受到的消息"
        android:textSize="20sp"
        />

    <Button
        android:id="@+id/btn_finish"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回应答消息"
        />



</LinearLayout>

方法:改用

registerForActivityResult中的launch方法就好了,这样就不用手动判断请求码,结果码了,避免这个过程中的错误。

MainActivity.java

package com.example.apptest;

import static java.time.LocalTime.now;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_enable = findViewById(R.id.btn_enable);
        btn_enable.setOnClickListener(this);
        // 注册ActivityResultLauncher
        register = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    // 处理返回结果
                    if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                        handleResult(result.getData());
                    }
                }
        );

    }

    @Override
    public void onClick(View v) {
            if (v.getId() == R.id.btn_enable){
                TextView tv_send = findViewById(R.id.tv_send);
                //创建一个意图对象,准备跳到指定的活动页面
                Intent intent = new Intent(this,Activity2.class);
                //创建一个Bundle对象(创建一个新包裹),准备存储数据
                Bundle bundle = new Bundle();
                // 往包裹存入名为request_time的字符串
                bundle.putString("request_time",now().toString());
                // 往包裹存入名为request_content的字符串
                bundle.putString("request_content",tv_send.getText().toString());
                // 把快递包裹塞给意图
                intent.putExtras(bundle);
                //期望接收下个页面的返回数据。
                register.launch(intent);
            }
    }


    // 从下一个页面携带参数返回当前页面时触发。其中requestCode为请求代码,
// resultCode为结果代码,intent为下一个页面返回的意图对象


    protected void handleResult(Intent intent){
        TextView tv_response = findViewById(R.id.tv_response);
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                String response_time = bundle.getString("response_time");
                String response_content = bundle.getString("response_content");

                String desc = String.format("收到返回消息:\n应答时间为:%s\n应答内容为:%s", response_time, response_content);
                tv_response.setText(desc); // 把返回消息的详情显示在文本视图上
            }
        }
    }
}


Activity.java

package com.example.apptest;



import static java.time.LocalTime.now;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Activity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        TextView tv_receive = findViewById(R.id.tv_receive);
        Button btn_finish = findViewById(R.id.btn_finish);
        // 从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getExtras();
        // 从包裹中取出名为request_time的字符串
        String request_time = bundle.getString("request_time");
        // 从包裹中取出名为request_content的字符串
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s",request_time,request_content);
        tv_receive.setText(desc);

/*
下一个页面在返回上一个页面时,打包应答数据并调用setResult方法返回数据包裹。setResult
方法的第一个参数表示应答代码(成功还是失败),第二个参数为携带包裹的意图对象。
*/

        btn_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String response = "天气确实好,不过太阳太大了,不想出门,晚点再去打";
                Intent intent = new Intent();
                Bundle bundle2 = new Bundle();
                // 往包裹存入名为字符串
                bundle2.putString("response_time", now().toString());
                bundle2.putString("response_content", response);
                // 将包裹存入意图中
                intent.putExtras(bundle2);
                // 携带意图返回上一个页面。RESULT_OK表示处理成功
                setResult(Activity.RESULT_OK,intent);
                finish();// 结束当前的活动页面
            }
        });
        
    }
}

结果:

最后还是感谢评论区的那位网友!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值