Android Studio——猜数字游戏(Activity/intent 相关)

这次实验主要是涉及到以下知识点:

1.Intent使用

传值(当前Activity)
Intent intent = new Intent(this,OtherActivity.class);
Intent.putExtra(“key”,“value”);
startActivity(intent);

取值(目标Activity)
Intent intent = getIntent();
String name = intent .getStringExtra(“key”);

2.开始结束Activity

startActivity();
Finish();
那个可以从其他Activity传值给mainActivity的方法还没用过就不列了QAQ

3.计时器

我想的很简单QAQ,没有用到复杂的,就是在程序开始时获取当前时间,每次猜测结果后获取一下时间,两个一减就实现了计时。网上查了下可以用核心类 Timer 和 TimerTask实现,有兴趣的可以百度其用法。

最终实现的效果

下面是代码:

首先,activity_main.xml,主要用于用户输入所猜测的数字

<TextView
    android:id="@+id/txt"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@string/txt"></TextView>

<EditText
    android:id="@+id/input"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:autofillHints=""
    android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword"
    tools:targetApi="o"></EditText>

<Button
    android:id="@+id/submit"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@string/button"></Button>

activity_second.xml代码,主要用于显示用户猜测结果,所用时间及次数

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/txt1"></TextView>

    <EditText
        android:id="@+id/et_result"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:autofillHints=""
        android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword" tools:targetApi="o"></EditText>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/txt2"></TextView>

    <EditText
        android:id="@+id/et_time"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword"
        android:autofillHints="" tools:targetApi="o"></EditText>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/times"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/txt3"></TextView>

    <EditText
        android:id="@+id/et_times"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword"
        android:autofillHints="" tools:targetApi="o"></EditText>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/back"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/button1"></Button>
</LinearLayout>

ActivityMain.java代码
主要实现将第一个页面的要猜测的数字,用户猜测的数字,用户第一次猜测时所用时间,以及点击提交的次数(用于统计猜测次数)通过intent传给第二个activity

private Button submit;
private EditText input;
private  int hour,minute,second,count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.submit = (Button)findViewById(R.id.submit);
    this.input = (EditText)findViewById(R.id.input);
    Calendar calendar = Calendar.getInstance();
    //小时
     hour = calendar.get(Calendar.HOUR_OF_DAY);
    //分钟
    minute = calendar.get(Calendar.MINUTE);
    //秒
    second = calendar.get(Calendar.SECOND);
    final String target = Integer.toString((int)((Math.random()*9)+1));
    submit.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onClick(View v) {
            count++;
            Intent intent = new Intent();
            intent.setClass(MainActivity.this,SecondActivity.class);
            intent.putExtra("target",target);
            intent.putExtra("num",input.getText().toString());
            intent.putExtra("hour",Integer.toString(hour));
            intent.putExtra("minute",Integer.toString(minute));
            intent.putExtra("second",Integer.toString(second));
            intent.putExtra("count",Integer.toString(count));
            startActivity(intent);
        }
    });
}

SecondActivity.java
主要是通过Intent接收第一个activity所传数据,进行处理后,得到猜测结果,猜测所用时间,猜测次数,将其显示出来

private EditText et_result,et_time,et_times;
private Button back;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    this.et_result = (EditText)findViewById(R.id.et_result);
    this.et_time = (EditText)findViewById(R.id.et_time);
    this.et_times= (EditText)findViewById(R.id.et_times);
    this.back = (Button)findViewById(R.id.back);
    Intent intent = getIntent();
     int target =Integer.parseInt(intent.getStringExtra("target"));
     int num = Integer.parseInt(intent.getStringExtra("num"));
     int hour = Integer.parseInt(intent.getStringExtra("hour"));
     int minute = Integer.parseInt(intent.getStringExtra("minute"));
    int second = Integer.parseInt(intent.getStringExtra("second"));
    int count = Integer.parseInt(intent.getStringExtra("count"));
    //当前时间
    Calendar calendar = Calendar.getInstance();
    //小时
    int hour1 = calendar.get(Calendar.HOUR_OF_DAY);
    //分钟
    int minute1 = calendar.get(Calendar.MINUTE);
    //秒
    int second1 = calendar.get(Calendar.SECOND);
    int secondfinal = 0;//记录显示的秒数
    secondfinal = (hour1-hour)*3600+(minute1-minute)*60+second1-second;
    et_time.setText(Integer.toString(secondfinal)+"ms");
    et_times.setText(Integer.toString(count)+"次");
     if (target > num )
     {
         et_result.setText("猜小啦!");
     }
     else if(target == num)
     {
         et_result.setText("恭喜你猜对啦~~");
     }
     else
     {
         et_result.setText("猜大啦!");
     }
     back.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
           finish();
         }
     });
}

AndroidManifest.xml
主要不要忘记注册activity,除了mainactivity,其他都需要注册,不然会出错

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SecondActivity" >
        <intent-filter>
            <action android:name="android.intent.action.Second" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

本次实验依旧有很多BUG(QAQ什么时候可以拥有一个无BUG的至少自己发现不了BUG的应用啊~)BUG如下:
(1)整个程序一经开始就只会有产生一个猜测的数进行一轮猜测。
(2)另外用视频里的设置secondActivity的主题为弹窗模式,没有成功,参考网上的方式也木有成功(https://www.jianshu.com/p/64b399dfcab0),不知道是不是弹窗模式和什么组件有冲突。

另外其实不是很懂当secondActivity结束之后回到MainActivity,代码会从哪边开始,试图用debug看没有成功QAQ太菜了

好啦,结束啦,希望还会有下一次的Android学习记录鸭!

猜你喜欢

转载自www.cnblogs.com/pjshhh/p/12450573.html