Introduced:
feature Description: Through a Activity the three buttons below, respectively, are sending messages ( Sendbutton ), Chat history ( Chatbutton ), common language (Commonbutton) . When the button is clicked, toggle the fragmentabove todisplay different content.
Knowledge points used: When you click the Send Message button:
1. transfer The values from the EditText to the fragment from the mainactivity the.
2. How the fragment is dynamically displayed in the mainactivity.
for the first question: Sendbutton in the event of a click:
Private Onclicklistener Sendlistener = new Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
// Get Fragment1 in the text Control , can be in mainactivity Direct Access in Fragment the controls in
TextView showview= (TextView) Findviewbyid (R.ID.SHOWTEXTVIEWID);
// Get EditText in the content
String message = Edittext.gettext (). toString ();
Getintent (). PutExtra ("str", message);
String temp = getintent (). Getstringextra ("str");
str = str.append (message). Append ("\ n");
String Text=null;
if (Str.length () >100) {
Text = str.substring (Str.length () -100, Str.length ()-1). ToString ();
}else {
Text = Str.tostring ();
}
// place the obtained value into the Intent to facilitate later Fragment obtained in
Getintent (). PutExtra ("Chatstr", str.tostring ());
Showview.settext (text);
}
};
For a second question:
// affirm Fragmentmanager objects, and fragmenttransaction
Private Fragmentmanager Manager;
Private fragmenttransaction transaction;
// methods to get two objects
Manager = Getsupportfragmentmanager ();
Transaction = Manager.begintransaction ();
// You must first initialize a Fragment To prevent access to the corresponding Fragment in the control
// will be loaded into the mainactivity Fragment instantiating an object
Fragment_sendcontent f1 = new Fragment_sendcontent ();
// The first parameter is Main_activity.xml in the Framelayout the Object
Transaction.add (r.id.framelayout_content, F1);
// add to the background stack
Transaction.addtobackstack (NULL);
// transaction must be committed
Transaction.commit ();
When you click the Chat log button: behavior: Gets all previous sent content.
The knowledge points used are:
1. How to get the data in mainactivity (the data you just sent is in an array in mainactivity)
2. switch from one fragment to another fragment .
For the first question:
First Look at Sendlistener in the event:
// place the obtained value into the Intent to facilitate later Fragment obtained in
Getintent (). PutExtra ("Chatstr", str.tostring ());
in the historical record of Fragment.java Medium:
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
TODO auto-generated Method Stub
// load the corresponding Fragment , here is History_record
View Contentview = inflater.inflate (R.layout.history_record, NULL);
Contentview.setbackgroundcolor (Color.magenta);
// get this Fragment methods of the controls in
TextView = (TextView) Contentview.findviewbyid (R.ID.RECORDTEXTVIEWID);
// get the data in Mainactivity
String chatstring = getactivity (). Getintent (). Getstringextra ("Chatstr");
Textview.settext (chatstring);
return contentview;
}
For a second question:
Fragment The dynamic switching:
Private Onclicklistener Chatlistener = new Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Fragment_historyrecord Historyrecord = new Fragment_historyrecord ();
Transaction = Manager.begintransaction ();
Transaction.replace (R.id.framelayout_content, Historyrecord);
Transaction.addtobackstack (NULL);
Transaction.commit ();
}
};
When you click the Common use button: phenomenon: Pop up frequently used words (the most troublesome to achieve).
The knowledge points used are:
1. use the ListView control to display common language
2. How to place data in a ListView
3. familiar with simpleadapter,map<k,v>,arraylist<e> The use of mates between
4. switch from one fragment to another fragment . (Not repeat)
For the first question:
Create a new XML file, plus a ListView can be.
<listview
Android:id= "@+id/commonlanguageid"
Android:layout_width= "Match_parent"
android:layout_height= " 200dp" >
</ListView>
For a second question:
// Three global variables
Private ListView ListView;
Private string[] STRs;
Private EditText EditText;
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
View Contentview = inflater.inflate (R.layout.fragment_listview, NULL);
// Get Fragment the control object in
ListView = (ListView) Contentview.findviewbyid (R.id.commonlanguageid);
// First parameter:
// The second parameter: is a list<e> Parameters
// the third parameter: Another layout file with a TextView control to display each bar ListView information in
// The fourth parameter:
// The fifth parameter:
Simpleadapter simpleadapter = new Simpleadapter (getactivity (). Getapplicationcontext (),
GetData (), R.layout.show_item, new string[]{"common"}, new Int[]{r.id.item});
Listview.setadapter (Simpleadapter);
return contentview;
}
// Add a Few common phrases that you have stated to ArrayList , use key-value pairs
Public arraylist
arraylist
STRs = new string[]{
" Children "," Miss "," Little Dong Wang "," Jiangxi "," Hi there! "
};
for (int i=0;i<strs.length;i++) {
hashmap<string, string> map = new hashmap<> ();
Map.put ("Common", strs[i]);
List.add (map);
}
return list;
}
// when you click each bar Item is the event that is triggered
public class listener{
Public Onitemclicklistener ItemListener = new Onitemclicklistener () {
@Override
// The third parameter is the one that gives Item in the ListView the location
public void Onitemclick (adapterview<?> arg0, View arg1, int position,
Long Arg3) {
TODO auto-generated Method Stub
// Get mainactivity the control object in
EditText = (editText) getactivity (). Findviewbyid (R.id.edittextid);
String string = Strs[position];
Edittext.settext (string);
}
};
// The above Click event can only occur in the Onresume () , cannot be placed in Oncreatview () in
@Override
Public void Onresume () {
TODO auto-generated Method Stub
Super.onresume ();
Listview.setonitemclicklistener (New Listener (). ItemListener);
}