package com.example.chongzhi; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v) { EditText number = (EditText) findViewById(R.id.number); String s = number.getText().toString(); if (s.length() == 11) { Intent intent = new Intent(this, activity_main2.class); intent.putExtra("number", s); startActivityForResult(intent, 1); }else{ Toast.makeText(this, "请输入11位的电话号码", 0).show();; } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == 2) { String data1 = data.getStringExtra("fcz"); TextView fcz = (TextView) findViewById(R.id.fcz); fcz.setText(data1); String data2 = data.getStringExtra("sb"); TextView qx = (TextView) findViewById(R.id.qx); qx.setText(data2); } } }
package com.example.chongzhi; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.EditText; public class activity_main2 extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent(); EditText jnumber = (EditText) findViewById(R.id.jnumber); jnumber.setText(intent.getStringExtra("number") + "的号码充值"); } public void click1(View v) { EditText je = (EditText) findViewById(R.id.je); String s = je.getText().toString(); Intent intent = new Intent(this, MainActivity.class); intent.putExtra("fcz", "成功充值" + s + "块钱"); setResult(2, intent); finish(); } public void click2(View v) { Intent intent = new Intent(this, MainActivity.class); intent.putExtra("sb", "充值失败"); setResult(2, intent); finish(); } }
<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" 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.day10_20.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="手机号码" android:textSize="20dp" /> <TextView android:id="@+id/qx" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_below="@+id/fcz" android:layout_marginTop="35dp" android:layout_toLeftOf="@+id/cz" android:ems="10" android:textSize="10dp" /> <EditText android:id="@+id/number" android:layout_width="200dp" android:layout_height="30dp" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="14dp" android:ems="10" android:phoneNumber="true" android:textSize="10dp" > <requestFocus /> </EditText> <Button android:id="@+id/cz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/number" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:onClick="click" android:text="充值" /> <TextView android:id="@+id/fcz" android:layout_width="100dp" android:layout_height="30dp" android:layout_alignLeft="@+id/qx" android:layout_below="@+id/cz" android:layout_marginTop="54dp" android:ems="10" android:textSize="10dp" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="你要为:" android:textSize="20dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="充值金额:" android:textSize="20dp" /> <EditText android:id="@+id/jnumber" android:layout_width="200dp" android:layout_height="30dp" android:layout_alignBottom="@+id/textView1" android:layout_toRightOf="@+id/textView1" android:textSize="10dp"/> <EditText android:id="@+id/je" android:layout_width="100dp" android:layout_height="30dp" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_centerHorizontal="true" android:textSize="10dp" /> <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/je" android:layout_marginLeft="38dp" android:layout_marginTop="145dp" android:onClick="click1" android:text="充值" /> <Button android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/b1" android:layout_alignBottom="@+id/b1" android:layout_alignRight="@+id/jnumber" android:onClick="click2" android:text="取消充值" /> </RelativeLayout>
4.6使用Fragment
4.6.1 Fragment简介
Fragment(碎片)嵌入Activity中的UI片段,描述部分布局,一个Activity可以包含多个Fragment,一个Fragment也可以在多个Activity中使用。
4.6.2 Fragment的生命周期
(1)onAttach()建立关联时调用(2)onCreateView()创建试图调用(3)onActivityCreate()相关联的Activity已创建完成时调用
(4)onDestoryView()关联的视图被移除时调用(5)onDetach()接触关联时调用
4.6.3 创建Fragment
public class NewListFragment extends Fragment { public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle saveInstanceState) { View v= inflater.inflate(R.layout.fragment, container, false); return v; } }
4.6.4 在Activity中添加Fragment
1.在布局文件中添加Fragment
<fragment android:name="com.example.NewListFargment" android:id="@+id/newlist" android:layout_width="match_parent" android:layout_height="match_parent" />
2.在Activity中动态加载Fragment
(1)创建一个Fragment的实例对象
(2)获取FragmentManager的实例
(3)开启FragmentTransaction(事务)
(4)向Activity的布局容器中添加Fragment
(5)通过commit()方法提交事务
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //在Actvivity中动态添加Fragment /*步骤:A:创建一个Fragment的实例对象 B:获取FragmentManager(Fragment管理器)的实例 C:开启FragmentTransaction(事务) D:向Activity的布局容器(一般为FrameLayout)中添加Fragment E:通过commit()方法提交事务 */ //代码如下: NewListFragment fragment=new NewListFragment();//实例化Fragment对象 FragmentManager fm= getFragmentManager();//获取FragmentManager的实例 //获取FragmentTransaction实例 FragmentTransaction bgt=fm.beginTransaction(); //bgt.replace(, fragment); }