我每 6 秒后使用处理程序调用特定方法。
代码:
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),"Logout" , Toast.LENGTH_LONG).show();
}
});
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
UpdateTask();
handler.postDelayed(this, 6000);
}
}, 1000);
现在的问题是,当我单击 textview tv (这让我吐司“注销”)时,当消息要读取时(或者当控件处于 UpdateTask() 方法中时,它会在 6 秒后单击注销。
无论代码在 UpdateTask() 方法中,我只想随时点击 textview“tv”。
我怎样才能做到这一点?
请帮忙。
编辑: ............
public class Messages extends Activity {
static int cnt=0;
//protected Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messages);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String id = intent.getStringExtra(MainActivity.EXTRA_ID);
String[] lst = null;
ListView lm=(ListView)findViewById(R.id.listView1);
TextView tv = (TextView)findViewById(R.id.textView1);
TextView tvNewMessages = (TextView)findViewById(R.id.tvNewMessages);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),"Logout" , Toast.LENGTH_LONG).show();
}
});
/*ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
// Log.i("hello", "world");
runOnUiThread(new Runnable() {
public void run() {
UpdateTask();
}
});
}
}, 0, 10, TimeUnit.SECONDS);*/
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
UpdateTask();
handler.postDelayed(this, 6000);
}
}, 1000);
tv.setText("Welcome " + message);
//handler.postDelayed(new UpdateTask(),500);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.messages, menu);
return true;
}
public void UpdateTask() {
// TODO Auto-generated method stub
try
{
// Toast.makeText(getApplicationContext(),"update " , Toast.LENGTH_LONG).show();
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String id = intent.getStringExtra(MainActivity.EXTRA_ID);
String[] lst = null;
String[] lstNew = null;
ListView lm=(ListView)findViewById(R.id.listView1);
TextView tv = (TextView)findViewById(R.id.textView1);
TextView tvNewMessages = (TextView)findViewById(R.id.tvNewMessages);
tv.setText("Welcome " + message);
CallSoap cs=new CallSoap();
lst=cs.GetMessage(id);
lstNew=cs.GetNewMessage(id);
final int numOfMessages=lstNew.length;
//Toast.makeText(getApplicationContext(),"Call "+lstNew.length , Toast.LENGTH_LONG).show();
//setListAdapter(new ArrayAdapter<String>(Messages.this, R.layout.simple_list_item_1,FRUITS));
// ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst);
/*for (int i=1;i<numOfMessages;i++)
{
tv.setText("Welcome " + i);
//lm.getChildAt(i).setBackgroundColor(Color.RED);
tv.setText("Welcome : " + i);
}
*/
tvNewMessages.setText("You have "+ numOfMessages +" new messages");
ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if(position<numOfMessages){
v.setBackgroundColor(Color.RED);
}
else
{
v.setBackgroundColor(Color.WHITE);
}
return v;
}
};
lm.setAdapter(adpt);
}
catch(Exception ex)
{
ex.printStackTrace();
//Toast.makeText(getApplicationContext(),"Call "+ex.getMessage() , Toast.LENGTH_LONG).show();
}
}
}