public class Test extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
// TODO Auto-generated method stub
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
showDialog(1);
break;
case R.id.button2:
showDialog(2);
break;
case R.id.button3:
showDialog(3);
break;
default:
break;
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1: {
Dialog dialog = new AlertDialog.Builder(Test.this).setTitle("Question")
.setMessage("one111111111111111111111")// 设置内容
.setPositiveButton("Yes", // 设置确定按钮
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setResult(RESULT_OK);
finish();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();// 创建
return dialog;
}
case 2: {
ScrollView sv = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText("2");
sv.addView(tv);
Dialog dialog2 = new AlertDialog.Builder(Test.this).setTitle("Question")
.setView(sv)// 设置内容
.setPositiveButton("Yes", // 设置确定按钮
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setResult(RESULT_OK);
finish();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();// 创建
return dialog2;
}
case 3: {
ScrollView sv = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText("3");
sv.addView(tv);
Dialog dialog3 = new AlertDialog.Builder(Test.this).setTitle("Question")
.setView(sv)// 设置内容
.setPositiveButton("Yes", // 设置确定按钮
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setResult(RESULT_OK);
finish();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();// 创建
return dialog3;
}
default: {
return null;
}
}
}
}
事实上就是发出一个Intent到Intent.ACTION_MAIN,一般都应用到Service中,注意要设置Flags
Intent i= new Intent(Intent.ACTION_MAIN); //模拟按下home键
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //服务里调用,必须加入new task标识
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}