Skip to content

Instantly share code, notes, and snippets.

@ts-3156
Created July 22, 2011 22:53
Show Gist options
  • Save ts-3156/1100623 to your computer and use it in GitHub Desktop.
Save ts-3156/1100623 to your computer and use it in GitHub Desktop.
Androidでダイアログを作るサンプルコード
package com.main;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText resultText = null;
ProgressDialog progressDialog = null;
int progressCount = 0;
Handler progressHandler = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultText = (EditText)findViewById(R.id.textResult);
Button button1;
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("Alert Dialog")
.setMessage("Alert Dialog with Three Buttons.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
resultText.setText("Alert Dialog [OK] pressed");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
resultText.setText("Alert Dialog [Cancel] pressed");
}
})
.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
resultText.setText("Alert Dialog [Neutral] pressed");
}
})
.create();
alertDialog.show();
}
});
final String[] dialogItems = new String[]{
"one",
"two",
"three",
"four"
};
Button button2;
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("Alert Dialog List")
.setItems(dialogItems, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
resultText.setText(dialogItems[which]);
}
})
.create();
alertDialog.show();
}
});
Button button3;
button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Progress Dialog");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
resultText.setText("Progress Dialog [OK] pressed at " + progressCount + "%");
}
});
progressDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
resultText.setText("Progress Dialog [Cancel] pressed at " + progressCount + "%");
}
});
progressCount = 0;
progressDialog.show();
progressDialog.setProgress(0);
// このメソッドを呼ぶことにより、プログレスバーがhandleMessage()をコールバックするようになる
progressHandler.sendEmptyMessage(0);
}
});
progressHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(progressCount >= 100){
progressDialog.dismiss();
resultText.setText("progress Dialog finished " + progressCount + "%");
}
else{
progressCount++;
progressDialog.incrementProgressBy(1);
progressHandler.sendEmptyMessageDelayed(0, 100);
}
};
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment