package com.example.xingpengfei.guessnumber;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int number;
private Button button1;
private Button button2;
private EditText editText;
private void testRandom()
{
Random random=new Random();
number=random.nextInt(100);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testRandom();
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
editText=(EditText)findViewById(R.id.edit_text);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void onClick(View v)
{
switch (v.getId()){
case R.id.button1:
//String input=editText.getText().toString();
int input=0;
if(input==number)
Toast.makeText(MainActivity.this,"恭喜你猜对了!",Toast.LENGTH_SHORT).show();
else if(input>number)
Toast.makeText(MainActivity.this,"您猜的数字偏大!",Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,"您猜的数字偏小!",Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
testRandom();
break;
default:
break;
}
}
}
这是完整代码
在 button1.setOnClickListener(this);
button2.setOnClickListener(this);
这里的this报错,请指教该如何改?