交互对话框
使用Android SDK中,具有交互功能的对话框是AlertDialog窗口。
package com.example.test8; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { private Button bt; int n=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt=(Button)findViewById(R.id.button1); bt.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub n++; String str=Integer.toString(n); bt.setText("点击次数:"+n); new AlertDialog.Builder(MainActivity.this) .setTitle("提示") .setMessage("对话框") .setPositiveButton("确定",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } } ) .show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
拖放相片特效
在Android中,拖动图片的特效可以通过Android.content.Context,Android,widget.BaseAdapter和Android.widget.imageView等来实现。
在下面的列子中,context可以像画布一样,随时可以被处理覆盖。Context是作为Android.content子类。通过widget.BaseAdapter作为容器来存放Gallery所需要的图片。
这里是通过使用Gallery实现的相片拖拽功能,具体代码如下:
package com.example.test9; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.TextView; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { private TextView textview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview = (TextView)findViewById(R.id.textView1); textview.setText("特效演示"); textview.setTextColor(Color.BLUE); Gallery gallery=(Gallery)findViewById(R.id.gallery1); gallery.setAdapter(new ImageAdapter(this)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public class ImageAdapter extends BaseAdapter{ private Context myContext; private int[] myImageID={android.R.drawable.btn_minus, android.R.drawable.btn_radio, android.R.drawable.ic_lock_idle_low_battery, android.R.drawable.ic_menu_camera }; public ImageAdapter(Context c){ this.myContext=c; } public int getCount() { // TODO Auto-generated method stub return this.myImageID.length; } public Object getItem(int position) { // TODO Auto-generated method stub return position; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView i=new ImageView(this.myContext); i.setImageResource(this.myImageID[position]); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setLayoutParams(new Gallery.LayoutParams(120,120)); return i; } } }
设置about关于信息
创建一个onCreateOptionsMenu(Menu menu)类函数,用于添加Menu菜单项,再利用onOptionItemSelected选择菜单项
package com.example.test10; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,0,0,"关于"); menu.add(0,1,1,"退出"); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item){ super.onOptionsItemSelected(item); switch(item.getItemId()){ case 0: openOptionsDialog(); break; case 1: finish(); } return true; } private void openOptionsDialog(){ new AlertDialog.Builder(this) .setTitle("关于") .setMessage("***") .setPositiveButton("OK",new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .show(); } }
程序加载中状态框
程序加载中的提示,此功能是通过Progress Dialog类来运行,此类在Android.app.ProgressDialog类里。但是,我们需要注意使用dismiss()函数来关闭取得焦点的对话框,否则程序将陷入死循环,也不能在线程里更改CONTEXT或parent view的任何状态。
package com.example.test11; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { private TextView text; private Button bt; public ProgressDialog myDialog=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text=(TextView)findViewById(R.id.textView1); bt=(Button)findViewById(R.id.button1); bt.setOnClickListener(myShowProgressBar); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } Button.OnClickListener myShowProgressBar=new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub final CharSequence strDialogTile="请等待"; final CharSequence strDialogBody="程序正在运行"; myDialog=ProgressDialog.show(MainActivity.this,strDialogTile,strDialogBody,true); text.setText(strDialogBody); new Thread(){ public void run(){ try{ text.setText("start"); sleep(3000); text.setText("end"); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally{ myDialog.dismiss(); } } }.start(); } }; }
本文 由 cococo点点 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可。欢迎转载,请注明出处:
转载自:cococo点点 http://www.cnblogs.com/coder2012
时间: 2024-11-03 20:53:41