您的位置:首页 > 移动开发 > Android开发

Android 闹钟 开发过程记录(三)

2015-03-01 12:17 513 查看
下面的布局是使用Activity用作弹出式对话框的样式

好处:

1、显示位置的设置,直接就是一个layout.xml随心所遇的感觉,可以实现在任何位置。

2、对对话框内控件的事件的处理,都独立出来在一个类中,调用的地方仅需要startActivity()就可以, 这样可以使代码结构上更加清晰。



具体实现如下:

1.在style.xml中定义一个style:

</style>
     <style name="MyButtomRepeatSettingDialog"> 
     <item name="android:windowIsTranslucent">true</item> 
     <item name="android:windowBackground">@color/translucent_background</item>
     <item name="android:windowNoTitle">true</item> 
     <item name="android:backgroundDimEnabled">true</item> 
     <item name="android:windowAnimationStyle">@style/Anim_translate</item>
 </style>


2.定义一个activity,并在清单文件中,使用上面定义的主题:

<activity
            android:name="cn.edu.usts.cardhelper.activities.AlarmClockRepeatDialogActivity"
            android:theme="@style/MyButtomRepeatSettingDialog" >
        </activity>


3.定义activity的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/bg_color"
        android:orientation="vertical"
        tools:ignore="UselessParent" >

        <TextView 
            android:id="@+id/tv_repeat_once"
            style="@style/tv_alarm_clock_repeat_dialog"
            android:text="@string/tv_alarm_clock_setting_repeat_once" />

        <View style="@style/dividing_line" />

        <TextView
            android:id="@+id/tv_repeat_everyday"
            style="@style/tv_alarm_clock_repeat_dialog"
            android:text="@string/tv_alarm_clock_setting_repeat_everyday" />

        <View style="@style/dividing_line" />

        <TextView
            android:id="@+id/tv_repeat_mon_to_fri"
            style="@style/tv_alarm_clock_repeat_dialog"
            android:text="@string/tv_alarm_clock_setting_repeat_mon_to_fri" />

        <View style="@style/dividing_line" />

        <TextView
            android:id="@+id/tv_repeat_custom"
            style="@style/tv_alarm_clock_repeat_dialog"
            android:text="@string/tv_alarm_clock_setting_repeat_custom" />

        <View style="@style/dividing_line" />
    </LinearLayout>

</RelativeLayout>


4.在activity的代码中实现相关功能:

public class AlarmClockRepeatDialogActivity extends Activity implements OnClickListener{

	private TextView tv_repeat_once;
	private TextView tv_repeat_everyday;
	private TextView tv_repeat_mon_to_fri;
	private TextView tv_repeat_custom;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_clock_repeat_dialog_activity);
		tv_repeat_once = (TextView) this.findViewById(R.id.tv_repeat_once);
		tv_repeat_everyday = (TextView) this.findViewById(R.id.tv_repeat_everyday);
		tv_repeat_mon_to_fri = (TextView) this.findViewById(R.id.tv_repeat_mon_to_fri);
		tv_repeat_custom = (TextView) this.findViewById(R.id.tv_repeat_custom);
		tv_repeat_once.setOnClickListener(this);
		tv_repeat_everyday.setOnClickListener(this);
		tv_repeat_mon_to_fri.setOnClickListener(this);
		tv_repeat_custom.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_repeat_once:	//只响一次
			Intent onceIntent = new Intent();
			onceIntent.putExtra("repeatCycle", this.getString(R.string.tv_alarm_clock_setting_repeat_once));
			setResult(0, onceIntent);
			finish();
			break;
		case R.id.tv_repeat_everyday:		//每天
			Intent EverydayIntent = new Intent();
			EverydayIntent.putExtra("repeatCycle", this.getString(R.string.tv_alarm_clock_setting_repeat_everyday));
			setResult(0, EverydayIntent);
			finish();
			break;
		case R.id.tv_repeat_mon_to_fri:	//周一至周五
			Intent monToFriIntent = new Intent();
			monToFriIntent.putExtra("repeatCycle", this.getString(R.string.tv_alarm_clock_setting_repeat_mon_to_fri_2));
			setResult(0, monToFriIntent);
			finish();
			break;
		case R.id.tv_repeat_custom:	//自定义
			Intent customIntent = new Intent(this, AlarmClockRepeatCustomActivity.class);
			startActivityForResult(customIntent, 0);
			break;

		}

	}
	
	/**
	 * 触摸该Activity布局控件以外的地方,自动销毁该Activity
	 */
	@Override 
	public boolean onTouchEvent(MotionEvent event){ 
		this.finish(); 
		return true; 
	} 

	/**
	 * 获取 自定义 的星期,并将结果返回给 闹钟设置界面,销毁该界面
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(data != null){
			Intent customIntent = new Intent();
			String repeatCycle = data.getStringExtra("repeatCycle");//得到 闹钟 自定义 重复周期
			customIntent.putExtra("repeatCycle", repeatCycle);
			setResult(0, customIntent);
		}
		finish();
	}
}


至此,一个使用activity作为弹出对话框的样式的效果就出来了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: