想要获取一个EditText中的值作为查询关键字,但是通过getText().goString()无法取到值,代码如下:
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
EditText t1=(EditText)findViewById(R.id.namequery);
Log.e("hello1","hhhhh");
Log.e("hello", t1.getText().toString().trim());
EditText t2=(EditText)findViewById(R.id.telquery);
EditText t3=(EditText)findViewById(R.id.emailquery);
Cursor c = getContentResolver().query(ContactsProvider.CONTENT_URI,
null, ContactColumn.NAME+"like '%"+t1.getText().toString()+"%'", null,null);
int count = c.getCount();
if(count!=0)
{
String nameinfo;
String phonenumber;
String emailinfo;
do
{
c.moveToFirst();
nameinfo=c.getString(1);
phonenumber=c.getString(2);
emailinfo=c.getString(3);
Toast.makeText(mycontact.this, nameinfo+phonenumber+emailinfo, Toast.LENGTH_LONG).show();
}while(c.moveToNext());
}
} 在LogCat中可以看到Hello1的值hhhh,但t1.getText().toString()的值却取不到,这是为什么啊,请教高人指点小弟,不胜感激!!
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
EditText t1=(EditText)findViewById(R.id.namequery);
Log.e("hello1","hhhhh");
Log.e("hello", t1.getText().toString().trim());
EditText t2=(EditText)findViewById(R.id.telquery);
EditText t3=(EditText)findViewById(R.id.emailquery);
Cursor c = getContentResolver().query(ContactsProvider.CONTENT_URI,
null, ContactColumn.NAME+"like '%"+t1.getText().toString()+"%'", null,null);
int count = c.getCount();
if(count!=0)
{
String nameinfo;
String phonenumber;
String emailinfo;
do
{
c.moveToFirst();
nameinfo=c.getString(1);
phonenumber=c.getString(2);
emailinfo=c.getString(3);
Toast.makeText(mycontact.this, nameinfo+phonenumber+emailinfo, Toast.LENGTH_LONG).show();
}while(c.moveToNext());
}
} 在LogCat中可以看到Hello1的值hhhh,但t1.getText().toString()的值却取不到,这是为什么啊,请教高人指点小弟,不胜感激!!
在LogCat中可以看到Hello1的值hhhh,但t1.getText().toString()的值却取不到,这是为什么啊,请教高人指点小弟,不胜感激!!
这个是肯定的呀,你的onClick()种根本没对你的 EditText t1=(EditText)findViewById(R.id.namequery);进行设置文字,而是直接执行了 Log.e("hello1","hhhhh"); Log.e("hello", t1.getText().toString().trim());,所以打印出来的就是空,如果说你点击editText就刚好触发了onClick()这个方法。