버튼을 클릭 할 때 대화 상자를 열고 싶습니다. 이 대화 상자에서 루프를 통해 올 텍스트를 동적으로 (스톱워치와 같이) 텍스트를 설정하려고합니다. 누군가 샘플 코드를 안내해 주시겠습니까? 나는 그물에 주어진 많은 예제를 시도했지만 성공적으로 결과를 얻을 수 없습니다.Android 비동기 작업 사용
//Button where the action starts
public void onClickStart(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_details);
dialog.setTitle("Your Step Details");
dialog.show();
DisplayTask dd= new DisplayTask();
dd.execute();
}
public void doWork(){
final Handler handler=new Handler();
new Thread(new Runnable(){
boolean isRunning=true;
@Override
public void run() {
while(isRunning){
try{
handler.post(new Runnable(){
@Override
public void run() {
try{
TextView txtCurrentTime= (TextView)findViewById(R.id.txtLeft);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":" + minutes + ":" + seconds;
txtCurrentTime.setText(curTime);
}catch (Exception e) {}
}
});
}catch(Exception e){
}
}
}
}).start();
}
public class DisplayTask extends AsyncTask<Void , Void, Void> {
protected void onPostExecute(){
MainActivity main= new Mai`enter code here`nActivity();
main.doWork();
}
@Override
protected Void doInBackground(Void... params) {
onPostExecute();
return null;
}
}
시도한 내용 - – minhaz
버튼을 클릭 할 때마다 메서드가 호출되었습니다. 대화 상자가 열렸습니다. 이 대화 상자에는 두 개의 텍스트 상자가 있습니다. 동적 텍스트로 텍스트를 설정하고 싶습니다 (시간을 말합니다). asyncTask 클래스를 만들고 onPostExecute()에서 textBox를 업데이트하는 메서드를 호출했습니다. 하지만 작동하지 않습니다 – DebD
코드를 게시 할 수 있습니까? 그러면 우리는 당신이 잘못한 것을 볼 수 있습니다. – Jules