android001 helloworld-GUESSNUMBER

Android Studio一个窗口只能有一个项目,而Eclipse则可以同时存在很多项目,如果你看着不习惯可以点击左上角的Android机器人进行切换:

将"Android"切换为"Project"

if(edtNumber.getText().toString().equals("")){
    txvHint.setText("please fill number");

active_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="name.yangxuan.guessnumber.MainActivity">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/btnGenNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/btnGuess_color"
            android:onClick="clickButton"
            android:text="generate" />

        <EditText
            android:id="@+id/edtNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/edtNumber_hint_text"
            android:inputType="number"
            android:textSize="@dimen/text_size" />

        <Button
            android:id="@+id/btnGuess"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/btnGuess_color"
            android:onClick="clickButton"
            android:text="@string/btnGuess_text"
            android:textSize="@dimen/text_size" />

        <TextView
            android:id="@+id/txvHint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/txvHint_color"
            android:textSize="@dimen/text_size" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActicvity

package name.yangxuan.guessnumber;

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private int randomNumber=0;
    private int number=0;
    private EditText edtNumber;
    private TextView txvHint;

    @Override
    //在项目启动时执行的方法onCreate
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtNumber = (EditText)findViewById(R.id.edtNumber);
        txvHint = (TextView)findViewById(R.id.txvHint);
    }
    //
    public void clickButton(View view){

        switch (view.getId()){
            case R.id.btnGenNumber:
                randomNumber = (int)new Random().nextInt(100);
                txvHint.setText("alread genera a random number,please fill number in "+randomNumber);

                break;
            case R.id.btnGuess:
                if(edtNumber.getText().toString().equals("")){
                    txvHint.setText("please fill number");
                }
                else{
                    number = Integer.parseInt(edtNumber.getText().toString());
                    txvHint.setText(checkNumber(number,randomNumber));
                }

                System.out.println("number:"+number+"randomNumber:"+randomNumber);
            break;

        }
    }

    private String checkNumber(int number,int randomNumber){
        Resources res = getResources();
        String hint="";
        if(number>randomNumber)
            hint = String.format(res.getString(R.string.hint_hight),number);
        else if(number<randomNumber)
            hint = String.format(res.getString(R.string.hint_low),number);
        else
            hint = String.format(res.getString(R.string.hint_right));
        return hint;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值