dialog中的EditText中的内容为什么获取不到,代码总是报错,就大神帮忙

傲儿our 发布于 2014/10/05 21:53
阅读 681
收藏 0

代码如下,但是String name = nameet.getText().toString(); 这句总报错,在nameet下有红色的波浪线,错误为nameet cannot be resolved

private void victory(){ 

//Intent intent = new Intent(); 
   //intent.putExtra("lock", "1"); 
//setResult(RESULT_CODE, intent); 
android.content.DialogInterface.OnClickListener listener= new MyListener(); 
EditText nameet = new EditText(this); 
new AlertDialog.Builder(this)    
.setTitle("成功啦!") 
.setMessage("敢问大侠名号?") 
//.setIcon(android.R.drawable.ic_dialog_info)    
.setView(nameet)    
.setPositiveButton("挑战下一关", listener)    
.setNegativeButton("选择关卡", listener)    
.show(); 

public class MyListener implements 
android.content.DialogInterface.OnClickListener { 
public void onClick(DialogInterface dialog, int which) { 

switch(which){ 
case AlertDialog.BUTTON_POSITIVE:{ 
//比较,写入最高分数 
if(current_score>record.getBestScode()){ 
record.setBestScode(current_score); 
tv_bestScore.setText(current_score+""); 
   } 
Toast.makeText(Play.this, name,Toast.LENGTH_LONG).show(); 
//存入玩家名号  
String name = nameet.getText().toString();  
//跳到下一关 
gradenum = gradenum * 2; 
Intent intent = new Intent(); 
   intent.putExtra("gradelock", gradenum+""); 
setResult(RESULT_CODE1, intent); 
finish(); 
break; 


//选择关卡 
case AlertDialog.BUTTON_NEGATIVE:{ 
//比较,写入最高分数 
if(current_score>record.getBestScode()){ 
record.setBestScode(current_score); 
tv_bestScore.setText(current_score+""); 

gradenum = gradenum * 2; 
Intent intent = new Intent(); 
   intent.putExtra("gradelock", gradenum+""); 
setResult(RESULT_CODE2, intent); 
finish(); 
break; 

加载中
0
carlos
carlos
把你报错的log贴出来看看才知道啊
0
Kinegratii
Kinegratii
nameet是局部变量,应该把按钮监听写成匿名类,这样就可以引用到了
0
zhx2012
zhx2012
EditText nameet = new EditText(this);      之前加个final试试   或定义成全局变量
0
k
kingdawin
nameet不能是局部变量,要定义成全局变量,你在方法外部声明该变量
返回顶部
顶部