<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                1、在實際應用中,我們不僅要向Activity中傳數據,也要從Activity中返回數據。雖然傳遞數據和返回數據類似,也可以采用前面四篇中提到的4種方法,但是一般建議采用Intent對象的方式來返回數據,使用這種方式返回數據,需要使用startActivityForResult方法來顯示Activity; 2、新建Android項目“android_intent_forresult”,打開布局文件“activity_main.xml”,添加“LinearLayout”、“TextView”、“EditView”等標簽,代碼如下: ~~~ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/one" android:layout_width="20dp" android:layout_height="wrap_content" > </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" + " > </TextView> <EditText android:id="@+id/two" android:layout_width="20dp" android:layout_height="wrap_content" > </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" = " > </TextView> <EditText android:id="@+id/result" android:layout_width="20dp" android:layout_height="wrap_content" > </EditText> </LinearLayout> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="計算結果" > </Button> </LinearLayout> ~~~ 效果: ![](https://box.kancloud.cn/2016-08-23_57bc06b2911c2.jpg) 3、新建布局文件“other.xml”,添加“TextView”、“EditView”、“Button”標簽,代碼如下: ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/msg" android:layout_width="match_parent" android:layout_height="wrap_content" > </TextView> <EditText android:id="@+id/three" android:layout_width="match_parent" android:layout_height="wrap_content" > </EditText> </LinearLayout> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="返回結果" > </Button> </LinearLayout> ~~~ 4、新建“OtherActivity.java”文件,并使其繼承“Activity”,添加“onCreate”方法,代碼如下: ~~~ package com.android.myintent; import android.app.Activity; import android.os.Bundle; public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); } } ~~~ 5、在“AndroidManifest.xml”清單文件中加入“Activity”,加入代碼: ~~~ <activity android:name=".OtherActivity" > </activity> ~~~ 6、在“Main.java”中添加Button成員和“setOnClickListener”,實現兩個Button的跳轉,點擊第一個Activity后,出現第二個Activity;在此方法內部創建意圖,用“startActivityForResult”啟動意圖,并在Main類里重寫“onActivityResult”;添加“EditText”成員,實現數據的輸入并傳入Intent中。代碼如下: ~~~ package com.android.myintent; import android.R.integer; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main extends Activity { private Button button; private final static int REQUESTCODE = 1;// 表示返回的結果碼 private EditText one, two, result; // 數據輸入 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); one = (EditText) this.findViewById(R.id.one); two = (EditText) this.findViewById(R.id.two); result = (EditText) this.findViewById(R.id.result); button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // 點擊后獲得用戶錄入的值 int a = Integer.parseInt(one.getText().toString()); int b = Integer.parseInt(two.getText().toString()); // 創建意圖 Intent intent = new Intent(Main.this, OtherActivity.class); // 將值傳入意圖 intent.putExtra("a", a); intent.putExtra("b", b); startActivityForResult(intent, REQUESTCODE);// 表示可以返回結果 } }); } // 再重寫一個onActivityResult方法,作用是將當前Activity中的數據傳遞到另一個Activity的意圖中后,實現跳轉,再回傳回來。 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } ~~~ 7、在“OtherActivity.java”文件中添加Button和TextView成員,獲取意圖中的數據,代碼如下: ~~~ package com.android.myintent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; public class OtherActivity extends Activity { private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); // 實例化button和textview button = (Button) this.findViewById(R.id.button2); textView = (TextView) this.findViewById(R.id.msg); Intent intent = getIntent(); // 獲取Intent // 取出Intent中的值 int a = intent.getIntExtra("a", 0); int b = intent.getIntExtra("b", 0); textView.setText(a + " + " + b + " = " + " ? "); } } ~~~ 運行一下,看下效果: ![](https://box.kancloud.cn/2016-08-23_57bc06b2c4c38.jpg) 點擊“計算結果”,跳轉到第二個Activity: ![](https://box.kancloud.cn/2016-08-23_57bc06b2ef364.jpg) 8、回到“Main.java”文件中,從OtherActivity中獲取數據并顯示,代碼如下: ~~~ package com.android.myintent; import android.R.integer; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main extends Activity { private Button button; private final static int REQUESTCODE = 1;// 表示返回的結果碼 private EditText one, two, result; // 數據輸入 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); one = (EditText) this.findViewById(R.id.one); two = (EditText) this.findViewById(R.id.two); result = (EditText) this.findViewById(R.id.result); button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // 點擊后獲得用戶錄入的值 int a = Integer.parseInt(one.getText().toString()); int b = Integer.parseInt(two.getText().toString()); // 創建意圖 Intent intent = new Intent(Main.this, OtherActivity.class); // 將值傳入意圖 intent.putExtra("a", a); intent.putExtra("b", b); startActivityForResult(intent, REQUESTCODE);// 表示可以返回結果 } }); } // 再重寫一個onActivityResult方法,作用是將當前Activity中的數據傳遞到另一個Activity的意圖中后,實現跳轉,再回傳回來。 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == 2) {// 如果第二個Activity(OtherActivity)正常結束(“2”為返回碼resultCode)。 if (requestCode == REQUESTCODE) {// 如果返回狀態為1,即成功返回,就在意圖的返回值中取出數據。 int three = data.getIntExtra("three", 0);// 從第二個Activity中返回意圖中的數據。 result.setText(String.valueOf(three)); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } ~~~ 9、在“OtherActivity.java”文件中,添加點擊Button事件,使數據回傳~,代碼如下: ~~~ package com.android.myintent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class OtherActivity extends Activity { private Button button; private TextView textView; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); // 實例化button和textview button = (Button) this.findViewById(R.id.button2); textView = (TextView) this.findViewById(R.id.msg); editText = (EditText) this.findViewById(R.id.three); Intent intent = getIntent(); // 獲取Intent // 取出Intent中的值 int a = intent.getIntExtra("a", 0); int b = intent.getIntExtra("b", 0); textView.setText(a + " + " + b + " = " + " ? "); // 添加點擊事件并回傳數據 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent();// 重新聲明一個意圖。 int three = Integer.parseInt(editText.getText().toString());// 獲取輸入的值。 intent.putExtra("three", three); // 將three回傳到意圖中。 // 通過Intent對象返回結果,調用setResult方法。 setResult(2, intent);// resultCode為大于1的數,隨意選取,為2即可。 finish();// 結束當前Activity的生命周期。 } }); } } ~~~ 10、運行,結果: (1)輸入2和3: ![](https://box.kancloud.cn/2016-08-23_57bc06b32712a.jpg) (2)單擊“計算結果”,跳轉: ![](https://box.kancloud.cn/2016-08-23_57bc06b34a6cf.jpg) (3)輸入5,單擊“返回結果”,數據回傳: ![](https://box.kancloud.cn/2016-08-23_57bc06b373635.jpg) 實現要點: (1)在“Main.java”中,創建Intent并啟動Activity,調用“startActivityForResult”,并定義當前請求碼; (2)重寫“onActivityResult”方法,并設置條件,若滿足返回碼值,則將第二個Activity中的數據傳回來,賦給當前Activity的“result”編輯框; (3)在“OtherActivity.java”中,再創建一個意圖,將數據填寫到意圖中,通過意圖將結果回傳(通過“setResult”方法); (4)結束當前Activity生命周期;
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看