fragment, switching between and activity in Android

Source: Internet
Author: User

Function: Via a three button under activity, which is send message (Sendbutton), chat record (Chatbutton), common language (Commonbutton) . When the button is clicked, toggle the fragment above to display 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.

2. How the fragment is dynamically displayed in the mainactivity.

For the first question: in the Sendbutton Click event:

Private Onclicklistener Sendlistener = new Onclicklistener () {

@Override

public void OnClick (View v) {

TODO auto-generated Method Stub

gets The text control in Fragment1, which can be obtained directly from the control in the fragment in Mainactivity

TextView showview= (TextView) Findviewbyid (R.ID.SHOWTEXTVIEWID);

get the content in EditText

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 in the intent for Easy access later in the fragment

Getintent (). PutExtra ("Chatstr", str.tostring ());

Showview.settext (text);

}

};

For a second question:

affirm the Fragmentmanager object, and fragmenttransaction

Private Fragmentmanager Manager;

Private fragmenttransaction transaction;

methods to get two objects

Manager = Getsupportfragmentmanager ();

Transaction = Manager.begintransaction ();

You must first initialize a fragment toprevent the control in the corresponding fragment from being fetched

Fragment instantiated object to be loaded into mainactivity

Fragment_sendcontent f1 = new Fragment_sendcontent ();

The first parameter is the framelayout object in main_activity.xml

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 the sendlistener event:

place the obtained value in the intent for Easy access later in the fragment

Getintent (). PutExtra ("Chatstr", str.tostring ());

In the Fragment.java of the history record :

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);

methods to get the controls in this fragment

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:

dynamic switching of fragment:

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 the use of simpleadapter and map<k,v>,arraylist<e>

4. switch from one fragment to another fragment . (not repeat)

For the first question:

Create a new XML file, plus a ListView .

<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 the control object in fragment

ListView = (ListView) Contentview.findviewbyid (R.id.commonlanguageid);

First parameter:

second parameter: is a list<e> parameter

Third parameter: Another layout file with a TextView control to display the information in each listview

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 to ArrayList , using key-value pairs

Public arraylist

arraylist

STRs = new string[]{

" child ", " Miss ", " Little Dong Wang ", " Jiangxi ", " Hello!" "

};

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 clicking each item is the event that is triggered

public class listener{

Public Onitemclicklistener ItemListener = new Onitemclicklistener () {

@Override

The third parameter represents the position of the item in the listView

public void Onitemclick (adapterview<?> arg0, View arg1, int position,

Long Arg3) {

TODO auto-generated Method Stub

get the control object in mainactivity

EditText = (editText) getactivity (). Findviewbyid (R.id.edittextid);

String string = Strs[position];

Edittext.settext (string);

}

};

The above Click event can only occur in Onresume () and cannot be placed in Oncreatview ( )

@Override

public void Onresume () {

TODO auto-generated Method Stub

Super.onresume ();

Listview.setonitemclicklistener (New Listener (). ItemListener);

}

fragment, switching between and activity in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.