一、ChooseMsgActivity的实现
1、布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.just.festival_sms.ChooseMsgActivity">
<ListView
android:id="@+id/id_lv_msgs"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_fab_toSend"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/icon_to_send"
app:backgroundTint="#0ddcff"
app:borderWidth="0dp"
android:layout_marginBottom="@dimen/fab_margin">
</android.support.design.widget.FloatingActionButton>
</RelativeLayout>
在这里布局中,需要注意两点(关于这两点,可以品味一下大神的博客
http://blog.csdn.net/lmj623565791/article/details/46678867):
① app:borderWidth="0dp"
如果不设置0dp,那么在4.1的sdk上 FAB 会显示为正方形,而且在5.0以后的sdk没有阴影效果。
② 预期效果FloatingActionButton会距离屏幕底部有一定的距离,但在实际开发中,在4.0的手机上不用单独设置就可以达到预期效果
但是在5.0的手机上如果不设置app:borderWidth的话会贴着手机的底部,没有预期的效果,因此在4.0和5.0的手机上设置的margin的值不能相同
处理方法:在src/main/res/values/dimens.xml中添加一行<dimen name="fab_margin">0dp</dimen>
(即默认的版本中)
然后src/main/res下新建一个values-v21的文件夹,在里面新增一个dimens.xml文件 (即5.0时)
<resources>
<dimen name="fab_margin">16dp</dimen>
</resources>
如图:
以及ListView的item的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sms_item">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="@+id/id_tv_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp"
android:id="@+id/id_btn_toSend"
android:drawableLeft="@drawable/icon_to_send"
android:layout_gravity="right"
android:text="发送"/>
</LinearLayout>
2、ChooseMsgActivity.java
public class ChooseMsgActivity extends AppCompatActivity {
private ListView mLvMsgs;
private FloatingActionButton mFabToSend;//点击之后转到编辑短信的界面
private ArrayAdapter<Msg> mAdapter;
private LayoutInflater mInflater;
private int mFestivalId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_msg);
mInflater=LayoutInflater.from(this);
mFestivalId=getIntent().getIntExtra(FestivalCategoryFragment.ID_FESTIVAL,-1);
setTitle(FestivalLab.getInstance().getFestivalById(mFestivalId).getName());
initViews();
initEvent();
}
private void initEvent() {
mFabToSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendMsgActivity.toActivity(ChooseMsgActivity.this,mFestivalId,-1);
}
});
}
private void initViews() {
mLvMsgs= (ListView) findViewById(R.id.id_lv_msgs);
mFabToSend= (FloatingActionButton) findViewById(R.id.id_fab_toSend);
mLvMsgs.setAdapter(mAdapter=new ArrayAdapter<Msg>(this,-1,
FestivalLab.getInstance().getMsgsByFestivalId(mFestivalId)) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_msg, parent, false);
}
TextView content = (TextView) convertView.findViewById(R.id.id_tv_content);
Button toSend = (Button) convertView.findViewById(R.id.id_btn_toSend);
content.setText(" " + getItem(position).getContent());
toSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendMsgActivity.toActivity(ChooseMsgActivity.this, mFestivalId, getItem(position).getId());
}
});
return convertView;
}
});
}
}
无论是点击FloatingActionButton还是选择LIstView中相应短信的Button都会跳转到SendMsgActivity,唯一的区别就是点击FloatingActionButton后在SendMsgActivity中的EditText中不会有事先加载好的祝福短信的内容,而是空白的。
二、SendMsgActivity的实现
1、布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SendMsgActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/id_et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="180dp"
android:textSize="14sp"
android:background="@drawable/sms_item"
android:gravity="left|top"
android:textColor="#777"/>
<Button
android:id="@+id/id_btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="添加联系人"/>
<com.example.just.festival_sms.view.FlowLayout
android:id="@+id/id_fl_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.just.festival_sms.view.FlowLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/id_fab_send"
android:src="@drawable/icon_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="@dimen/fab_margin"
app:backgroundTint="#0ddcff"
app:borderWidth="0dp">
</android.support.design.widget.FloatingActionButton>
<FrameLayout
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="@+id/id_layout_loading"
android:background="#33bbbbbb">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送中..."
android:layout_gravity="center_vertical"/>
</LinearLayout>
</FrameLayout>
</FrameLayout>
当显示id_layout_loading
布局时,表明短信正在发送,要屏蔽用户的点击操作,所以要加上android:clickable="true"
但是默认是不显示的,在SendMsgActivity中通过setVisibility(View.GONE)实现
FlowLayout用于展示添加的联系人。
2、SendMsgActivity.java
public class SendMsgActivity extends AppCompatActivity {
public static final String KEY_ID_FESTIVAL="FestivalId";
public static final String KEY_ID_MSG="MsgId";
private int mFestivalId;
private int mMsgId;
private Festival mFestival;
private Msg mMsg;
private EditText mEdMsg;
private Button mBtnAdd;
private FlowLayout mFlContacts;
private FloatingActionButton mFabSend;
private View mLayoutLoading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_msg);
initDatas();
initViews();
}
private void initViews() {
mEdMsg= (EditText) findViewById(R.id.id_et_content);
mBtnAdd= (Button) findViewById(R.id.id_btn_add);
mFlContacts= (FlowLayout) findViewById(R.id.id_fl_contacts);
mFabSend= (FloatingActionButton) findViewById(R.id.id_fab_send);
mLayoutLoading=findViewById(R.id.id_layout_loading);
mLayoutLoading.setVisibility(View.GONE);//隐藏mLayoutLoading
if(mMsgId!=-1) {
mMsg= FestivalLab.getInstance().getMsgByFestivalIdAndMsgId(mFestivalId,mMsgId);//这里不同于视频中的getMsgById(mMsgId)
mEdMsg.setText(mMsg.getContent());
}
}
private void initDatas() {
mFestivalId=getIntent().getIntExtra(KEY_ID_FESTIVAL,-1);
mMsgId=getIntent().getIntExtra(KEY_ID_MSG,-1);
mFestival=FestivalLab.getInstance().getFestivalById(mFestivalId);
setTitle(mFestival.getName());
}
public static void toActivity(Context context, int festivalId, int msgId) {
Intent intent=new Intent(context,SendMsgActivity.class);
intent.putExtra(KEY_ID_FESTIVAL,festivalId);
intent.putExtra(KEY_ID_MSG,msgId);
context.startActivity(intent);
}
}
可以看到,在SendMsgActivity中有一个静态方法,用于从某个Activity跳转到SendMsgActivity,那么这样做有什么好处呢?
答案,很简单,就是为了方便。因为从某个Activity跳转到SendMsgActivity所需要的参数是固定的且一定需要的,所以可以把方法写到目标的Activity类中(即SendMsgActivity),因此当某个Activity需要跳转到目标Activity时会比较容易,且参数不容易出错。
到此,第一部分就完成了,可以运行一下App看看效果哦^-*。