我使用处理程序在每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(它给我吐司“Logout”)时,它会在6秒后点击退出,当消息被读取时(或当控件在UpdateTask()方法中时。) / p>
我只是想在任何时候点击textview“tv”,无论代码是否在UpdateTask()方法中。
我怎样才能实现这个目标?
请帮忙。
修改 ............
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();
}
}
}
答案 0 :(得分:0)
在没有延迟
的情况下尝试handler.post(this, 6000);