Android实例-进入Activity获取Edit输入数字并比较大小Toast输出

学习目标:

  • 文本输入框和3个按钮,分别为“生成随机数”“提交”和“返回”。
  • 隐式跳转。点击“退出”按钮,退出APP。
  • 用户输入一 一个0-100的数字,然后点击“提交” 当该数大(小)于系统提供的随机数时,给出Toast提醒“您输入的数字大(小)了!”, 当输入的数字等于随机数时,Toast提醒“恭喜你,答对了!点击“返回”按钮,返回到Acitivity界面。
  • 要求Activity和Activity2里面均复写所有的生命周期函数,在函数内使用Log.i打印输出该函数名。在运行提示框中截图Log输出。

学习内容:

  1. UI页面设计
  2. 隐式跳转
  3. Toas输出
  4. Log打印

学习产出:

要设计两个UI界面 一个为进入程序时准备开始游戏页面,一个为进入游戏后比较大小页面

XML1

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".myGame">
    <Button
        android:id="@+id/btn_1"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="开始游戏"
        android:textSize="30dp"
        />
    <Button
        android:id="@+id/btn_2"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="退出"
        android:textSize="30dp"
        />

</LinearLayout>

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">
    <EditText
        android:id="@+id/ed"
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:textSize="30dp"
        android:layout_marginLeft="5dp"
        android:hint="请输入一个0-100的整数"
        android:numeric="integer"
        />
    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="生成随机数"/>
    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="提交"/>
    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="返回"/>
   

</LinearLayout>

Java1

package com.example.class8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class myGame extends AppCompatActivity {
    private Button btn_1,btn_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_game);
        btn_1=findViewById(R.id.btn_1);
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("game");
                startActivity(intent);

            }
        });
        //btn_2退出
        btn_2=findViewById(R.id.btn_2);
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.i("生命周期函数","——————————————————onCreat——————————————————");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.i("生命周期函数","——————————————————onResume——————————————————");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("生命周期函数","——————————————————onStop——————————————————");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("生命周期函数","——————————————————onRestart——————————————————");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("生命周期函数","——————————————————onPause——————————————————");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("生命周期函数","——————————————————onDestroy——————————————————");
    }
}

小细节 Log.d和Log.i

java Activity

 

package com.example.class8;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class game extends AppCompatActivity {
    private Button b1,b2,b3;
    int num=5;
    int n;
    EditText ed;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_two);
        Log.d("生命周期函数", "——————————————————onCreate——————————————————");


        b1=findViewById(R.id.b1);
        b2=findViewById(R.id.b2);
        b3=findViewById(R.id.b3);
        ed=findViewById(R.id.ed);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = (int) (Math.random() * 100);

                }


        });
        //点击提交 比大小 Toast输出
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=ed.getText().toString();
                n=Integer.parseInt(str);

                if(n>num){
                    Toast.makeText(getApplicationContext(),"您输入的数字大了!",Toast.LENGTH_LONG).show();
                }
                else if(n==num) {
                    Toast.makeText(getApplicationContext(),"恭喜你,答对了!",Toast.LENGTH_LONG).show();
                }
                else if(n<num){
                    Toast.makeText(getApplicationContext(),"您输入的数字小了!",Toast.LENGTH_LONG).show();
                }

            }
        });
        //返回
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.i("生命周期函数","——————————————————onCreat——————————————————");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.i("生命周期函数","——————————————————onResume——————————————————");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("生命周期函数","——————————————————onStop——————————————————");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("生命周期函数","——————————————————onRestart——————————————————");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("生命周期函数","——————————————————onPause——————————————————");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("生命周期函数","——————————————————onDestroy——————————————————");
    }


}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是实现步骤: 1. 在布局文件中添加一个 EditText 控件,用于显示备忘录内容,以及一个 Button 控件,用于保存备忘录。 2. 在 Java 代码中获取 EditText 控件的实例,并使用 SharedPreferences 存储备忘录内容。 3. 在 Button 的点击事件中,将 EditText 控件中的内容保存到 SharedPreferences 中。 下面是完整的实现步骤: 1. 在布局文件中添加以下代码: ``` <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入备忘录内容" /> <Button android:id="@+id/save_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存" /> ``` 2. 在 Java 代码中添加以下代码: ``` private EditText mEditText; private SharedPreferences mSharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText = findViewById(R.id.edit_text); mSharedPreferences = getSharedPreferences("memo", MODE_PRIVATE); String memo = mSharedPreferences.getString("memo_content", ""); mEditText.setText(memo); } @Override protected void onPause() { super.onPause(); String memo = mEditText.getText().toString(); SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString("memo_content", memo); editor.apply(); } ``` 3. 在 Button 的点击事件中添加以下代码: ``` Button button = findViewById(R.id.save_button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String memo = mEditText.getText().toString(); SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString("memo_content", memo); editor.apply(); Toast.makeText(MainActivity.this, "备忘录已保存", Toast.LENGTH_SHORT).show(); } }); ``` 这样,就实现了在已有的备忘录中添加可编辑功能并保存的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值