AlertDialog窗口(交互功能的对话框)
常用于“程序提示”,警告,确认等,与用户交互。
Activity.java文件:
Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("重要");
alertDialog.setMessage("这是对话框");
android.content.DialogInterface.OnClickListener OKListner=
new android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
//在这里设计当对话框单击手要运行的事件
}
};
alertDialog.setPositiveButton("确定", OKListner);
alertDialog.show();
Button与TextView交互(置换文字颜色的机关)
Activity.java文件:
private Button button;
private TextView textView;
private int[] Colors;
private int colornum;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUI();
colornum = 0;
OnClickListener buttonListener = new OnClickListener(){
public void onClick(View v) {
if(colornum==Colors.length)
colornum = 0;
if(colornum<Colors.length)
{
textView.setTextColor(Colors[colornum]);
colornum++;
}
}
};
button.setOnClickListener(buttonListener);
}
private void initUI() {
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textview);
Colors = new int[]{
Color.BLACK,Color.RED,Color.BLUE,Color.GREEN,
Color.MAGENTA,Color.YELLOW,Color.GRAY
};
}
获得颜色的另一方法:
<resources>
<color name=”red”>#ffff0000</color>
</resources>
Int color = getResources.getColor(R.color.red);
textView.setTextColor(color);
Typeface对象控制不同的文字字体
TextView
setTextSize改变文字大小
setTypeface改变字体
方一:
首先在assets底下创建一fonts文件夹
并放入要使用的字体文件(.ttf):assets/fonts/HandmadeTypewriter.ttf
并提供相对路径给creatFromAsset()来创建Typeface对象
textView.setTextSize(20);
textView.setTypeface(Typeface.createFromAsset(getAssets(),”fonts/HandmadeTypewriter.ttf”));
方法二:
Resources.SytledAttributes resFont = getContext().obtainStyledAttributes(attrs,R.styleable.UnicodeTextView);
String fontName = resFont.getString(R.styleable.UnicodeTextView_font);
If(fontName!=null)
{
//在这里处理更改字体
}
Gallery画廊:如iphone拖动相片特效
设计相册,图片类型的选择等,
所用技巧:android.content.Context,android.widget.BaseAdapter.android.widget.ImageView.
在Activity中,Context就如同是张Canvas画布,随时等着被处理或覆盖。
通过widget.BaseAdapter容器存放Gallery所需要的图片。
private TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.text_view);
textView.setTextColor(Color.BLUE);
//Gallery显示相册,ImageAdapter存放相册里的相片
((Gallery)findViewById(R.id.gallery)).setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter
{
//类成员context为Context父类
private Context context;
//使用android.R.drawable里的图片作为图库源
private int myImageIds[] = {
android.R.drawable.btn_minus,
android.R.drawable.btn_radio,
android.R.drawable.ic_lock_idle_low_battery,
android.R.drawable.ic_menu_camera
};
//构造只有一个参数,即要存储的Context
public ImageAdapter(Context c) {
this.context=c;
}
//返回所有已定义的图片数量
public int getCount() {ated method stub
return this.myImageIds.length;
}
//取得目前容器中的图像的数组ID
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//取得目前欲显示的图像View,传入数组ID值使之读取与成像
public View getView(int position, View convertView, ViewGroup parent) {
//创建一个ImageView对象
ImageView i = new ImageView(this.context);
i.setImageResource(this.myImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
//设置宽高
i.setLayoutParams(new Gallery.LayoutParams(120,120));
return i;
}
public float getScale(boolean focused,int offset)
{
return Math.max(0,1.0f/(float)Math.pow(2, Math.abs(offset)));
}
}
Menu功能菜单程序设计
Activity.java文件:
//创建Menu菜单的项目
public boolean onCreateOptionsMenu(Menu menu)
{
//menu.add(groupId, itemId, order, titleRes);
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();
break;
}
return true;
}
private void openOptionsDialog() {
Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("关于");
alertDialog.setMessage("这是一个menu");
alertDialog.setPositiveButton("确定",buttonListener);
alertDialog.show();
}
ProgressDialog
加载对话框
Activity.java文件:
private ProgressDialog myDialog;
public void onClick(View v) {
myDialog = ProgressDialog.show(MainActivity.this, "请稍等片刻。。。","正在执行前端程序", true);
new Thread()
{
public void run()
{
try
{
//这里写后台运行的代码
sleep(3000);
}
catch(Exception e)
{e.printStackTrace();}
finally
{
myDialog.dismiss();
}
}
}.start();
}
全屏幕以按钮覆盖,动态产生按钮并最大化
ProgressDialog myDialog = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("请按下我运行后台程序。。");
setContentView(button);
button.setOnClickListener(buttonListener);
}
private OnClickListener buttonListener = new OnClickListener(){
public void onClick(View v) {
myDialog = ProgressDialog.show(MainActivity.this, "请稍等片刻。。",
"正在运行后台程序");
new Thread()
{
public void run()
{
try
{
sleep(3000);
}catch(Exception e)
{e.printStackTrace();}
finally
{
myDialog.dismiss();
}
}
}.start();
}
};
具有选择功能的对话框 Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("按我开始选择"); android.content.DialogInterface.OnClickListener dialogListener = new android.content.DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { String[] array = getResources().getStringArray(R.array.items); new AlertDialog.Builder(MainActivity.this) .setMessage("你的选择是:"+ array[which]) .setNegativeButton("确认", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int whick) { //处理的事 } }) .show(); } }; alert.setItems(R.array.items,dialogListener); alert.show(); array.xml文件 <string-array name="items"> <item>萌点女仆餐厅</item> <item>一锅日式小火锅</item> <item>好吃巴西烧烤</item> </string-array> 主题实现 style.xml文件: <!-- 基础应用程式主题,为预设主题 --> <style name="Theme" parent="android:Theme"> </style> <!-- 变更应用程式的主题,使之具有translucent背景 --> <style name="Theme.Translucent"> <item name="android:windowBackground"> @drawable/translucent_background </item> <item name="android:windowNoTitle">false</item> <item name="android:colorForeground">@drawable/blue</item> <item name="android:colorBackground">@drawable/white</item> </style> <!-- 变更应用程式的主题,使之具有不同颜色背景且具有translucent背景 --> <style name="Theme.Translucent2"> <item name="android:windowBackground"> @drawable/pink </item> <item name="android:windowNoTitle">false</item> <item name="android:colorForeground">@drawable/darkgreen</item> <item name="android:colorBackground">@drawable/pink</item> </style> <!-- 变更应用程式的主题,使之具有透明transparent背景 --> <style name="Theme.Transparent"> <item name="android:windowBackground"> @drawable/transparent_background </item> <item name="android:windowNoTitle">true</item> <item name="android:colorForeground">@drawable/blue</item> <item name="android:colorBackground">@drawable/pink</item> </style> <style name="TextAppearance.Theme.PlainText" parent="android:TextAppearance.Theme"> <item name="android:textStyle">normal</item> </style> Java文件 /* * 应用透明背景的主题 * */ // setTheme(R.style.Theme_Transparent); /* * 应用布景主题1 */ // setTheme(R.style.Theme_Translucent); /* * 应用布景主题2*/ setTheme(R.style.Theme_Translucent2);
本文介绍了Android应用程序中常用的UI设计及交互方式,包括AlertDialog的使用方法、Button与TextView的交互功能、Gallery画廊效果实现、Menu菜单设计以及ProgressDialog加载对话框的应用等。此外,还涉及了如何通过样式文件定制主题,使应用界面更加个性化。
680

被折叠的 条评论
为什么被折叠?



