Edittext getText toString

时间:2012-12-12 19:04:02

标签: android

下面是我的EditText,我想把它输入一个String tel Toast将成功显示getText toString 但是,我没有看到在LogCat中输入tel的值。

     edittext = (EditText) findViewById(R.id.editTel);
     edittext.setOnEditorActionListener(new OnEditorActionListener() {  
         @Override  
         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
            Toast.makeText(Reserve.this, edittext.getText().toString(), Toast.LENGTH_SHORT).show();  
            tel = edittext.getText().toString();
             return true;  
         }  
     });

     Log.d("TEL", "The tel is: "+tel);

2 个答案:

答案 0 :(得分:3)

Log.d("TEL", "The tel is: "+tel);方法放在onEditorAction方法中:

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Toast.makeText(Reserve.this, edittext.getText().toString(),
                                 Toast.LENGTH_SHORT).show();
tel = edittext.getText().toString(); 
Log.d("TEL", "The tel is: "+tel);
return true;
}
});

因为在OnEditorActionListener

之外可能无法控制

答案 1 :(得分:1)

您只是调用一个方法来存储EditText对象内对象(OnEditorActionListener类)的引用。流程继续立即执行Log.d()方法,打印并清空¨tel。 OnEditorActionListener.onEditorAction()方法将在任何其他时间被调用,由用户操作触发,它将执行其中的代码。也许流程可以更清晰地写成:


OnEditorActionListener listener = new OnEditorActionListener() {  
         @Override  
         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
            Toast.makeText(Reserve.this, edittext.getText().toString(), Toast.LENGTH_SHORT).show();  
            tel = edittext.getText().toString();
             return true;  
         }  
     });

edittext = (EditText) findViewById(R.id.editTel);
edittext.setOnEditorActionListener(listener);
Log.d("TEL", "The tel is: "+tel);

这种方式更清楚,onEditorAction()方法完全独立于该段代码的正常执行流程。