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;
}
}