1. 程式人生 > >第一個關於控制元件的例項(對話方塊)

第一個關於控制元件的例項(對話方塊)

1.先設定背景聊天檔案的圖片

資源管理器中找到安裝檔案下的tools->draw9patch.bat的檔案,雙擊可以執行,在裡面開啟將背景圖片加入編輯,並儲存替換原來的圖片

2.設定依賴庫

在app/build.gradle中新增依賴庫(目的為了使用recyclerview這個控制元件

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'

testCompile 'junit:junit:4.12'
}
並同步
3.修改主介面的佈局內容
設定背景圖片,加入RecyclerView的控制元件(顯示訊息),EditText(編輯傳送內容),Button(點擊發送)控制元件
<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/message_left">

<android.support.v7.widget.RecyclerView

android:id="@+id/msg_receler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something here"
android:maxLines="2"/>
<Button

android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"/>
</LinearLayout>
</LinearLayout>
4.建立訊息的類(包含兩個欄位,content是內容,type是型別表示接受的訊息還是傳送的訊息)
public class Msg {
public static final int TYPE_RECEIVED=0;
public static final int TYPE_SENT=1;
private String content;
private int type;
public Msg(String content,int type)
{
this.content=content;
this.type=type;
}
public String getContent()
{
return content;
}
public int getType()
{
return type;
}
}

5.完成RecyclerView子項的佈局(新建佈局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<LinearLayout android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/message_left">//新增背景圖片表示左邊的訊息
<TextView android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>//放在左邊的訊息
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_left">//新增背景圖片表示右邊的訊息
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"/>//放在右邊的訊息
</LinearLayout>
</LinearLayout>
6.建立RecyclerView的介面卡類(MsgAdapter)
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {///繼承RecyclerView.Adapter的類
private List<Msg> mMsgList;//定義訊息列表
static class ViewHolder extends RecyclerView.ViewHolder{
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
public ViewHolder(View view){///建構函式內含有兩個文字框,兩個線性佈局
super(view);
leftLayout=(LinearLayout)view.findViewById(R.id.left_layout);///找到左邊的背景佈局圖片
rightLayout=(LinearLayout)view.findViewById(R.id.right_layout);
leftMsg=(TextView) view.findViewById(R.id.left_msg);//找到左邊的資訊
rightMsg=(TextView) view.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List<Msg> msgList)//建構函式從外部傳入初始化過的訊息
{
mMsgList=msgList;
}
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType)
{
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder,int position)
{
Msg msg=mMsgList.get(position);
if(msg.getType()==Msg.TYPE_RECEIVED)
{
holder.leftLayout.setVisibility(View.VISIBLE);
holder.rightLayout.setVisibility(View.GONE);
holder.leftMsg.setText(msg.getContent());
}
else if(msg.getType()==Msg.TYPE_SENT)
{
holder.rightLayout.setVisibility(View.VISIBLE);
holder.leftLayout.setVisibility(View.GONE);
holder.rightMsg.setText(msg.getContent());
}
}
@Override
public int getItemCount()
{
return mMsgList.size();
}

}
7.完成主活動
public class MainActivity extends AppCompatActivity {
private List<Msg> msgList=new ArrayList<>();//定義訊息列表
private EditText inputText;
private Button send;
private RecyclerView msgRecyclerView;
private MsgAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);///設定主活動的佈局
initMsgs();//將定義的訊息列表初始化內涵三個訊息
inputText=(EditText) findViewById(R.id.input_text);//將定義的編輯框從佈局中找到初始化
send=(Button)findViewById(R.id.send);//將定義的傳送按鈕Congo佈局中找到初始化
msgRecyclerView=(RecyclerView)findViewById(R.id.msg_reccler_view);//將定義的RecyclerView控制元件從佈局中找到初始化
LinearLayoutManager layoutManager=new LinearLayoutManager(this);///線性管理器
msgRecyclerView.setLayoutManager(layoutManager);///RecyclerView設定線性管理其
adapter=new MsgAdapter(msgList);//初始化介面卡的同時將三個訊息也就是那個訊息列表傳入建構函式。
msgRecyclerView.setAdapter(adapter);///顯示介面訊息
send.setOnClickListener(new View.OnClickListener(){//為傳送按鈕註冊事件
@Override
public void onClick(View v)
{
String content=inputText.getText().toString();///從編輯框獲取文字
if(!"".equals(content)){
Msg msg=new Msg(content,Msg.TYPE_SENT);
msgList.add(msg);//編輯不空,則將內容和型別打包為Msg,並新增到列表
adapter.notifyItemInserted(msgList.size()-1);
msgRecyclerView.scrollToPosition(msgList.size()-1);
inputText.setText("");//編輯框設定為空
}


}
});
}
private void initMsgs()//初識化訊息將三個訊息放入訊息列表中
{
Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3=new Msg("This is Tom.Nice talking",Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
}