程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 簡略懂得C#設計形式編程中的橋接形式

簡略懂得C#設計形式編程中的橋接形式

編輯:C#入門知識

簡略懂得C#設計形式編程中的橋接形式。本站提示廣大學習愛好者:(簡略懂得C#設計形式編程中的橋接形式)文章只能為提供參考,不一定能成為您想要的結果。以下是簡略懂得C#設計形式編程中的橋接形式正文


先給年夜家展現下後果展現圖:

1 簡介

假如想在Activity中獲得新翻開Activity 封閉後前往的數據,須要應用體系供給的startActivityForResult(Intent intent, int requestCode)辦法翻開新的Activity,新的Activity 封閉後會向後面的Activity傳回數據,為了獲得傳回的數據,必需在後面的Activity中重寫onActivityResult(int requestCode, int resultCode, Intent data)辦法。

public class MainActivity extends Activity {
private final static String TAG="MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)this.findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//獲得新翻開Activity封閉後前往的數據
//第二個參數為要求碼,可以依據營業需求本身編號
startActivityForResult(new Intent(MainActivity.this, OtherActivity.class), 1);
}
});
}
/**
* 為了獲得傳回的數據,必需在後面的Activity中(指MainActivity類)重寫onActivityResult辦法
*
* requestCode 要求碼,即挪用startActivityForResult()傳遞曩昔的值
* resultCode 成果碼,成果碼用於標識前往數據來自哪一個新Activity
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String result = data.getExtras().getString("result");//獲得新Activity 封閉後前往的數據
Log.i(TAG, result);
}
}

當新Activity封閉後,新Activity前往的數據經由過程Intent停止傳遞,android平台會挪用後面Activity 的onActivityResult()辦法,把寄存了前往數據的Intent作為第三個輸出參數傳入,在onActivityResult()辦法中應用第三個輸出參數可以掏出新Activity前往的數據。

2 setResult

應用startActivityForResult(Intent intent, int requestCode)辦法翻開新的Activity,新Activity封閉前須要向後面的Activity前往數據須要應用體系供給的setResult(int resultCode, Intent data)辦法完成:

public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Button btnClose=(Button)findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//數據是應用Intent前往
Intent intent = new Intent();
//把前往數據存入Intent
intent.putExtra("result", "My name is linjiqin");
//設置前往數據
OtherActivity.this.setResult(RESULT_OK, intent);
//封閉Activity
OtherActivity.this.finish();
}
});
}
}

setResult()辦法的第一個參數值可以依據營業須要本身界說,下面代碼中應用到的RESULT_OK是體系Activity類界說的一個常量,值為-1,代碼片段以下:

public class android.app.Activity extends ......{
public static final int RESULT_CANCELED = 0;
public static final int RESULT_OK = -1;
public static final int RESULT_FIRST_USER = 1;
}

解釋:當點擊“翻開新的Activity”按鈕,會跳轉到“我是新翻開的Activity”頁面;

當點擊“封閉”按鈕,封閉以後頁面,同時跳轉到“我是舊的Activity”頁面,且會傳遞result參數給前一個Activity

3要求碼的感化

應用startActivityForResult(Intent intent, int requestCode)辦法翻開新的Activity,我們須要為startActivityForResult()辦法傳入一個要求碼(第二個參數)。要求碼的值是依據營業須要由自已設定,用於標識要求起源。例如:一個Activity有兩個按鈕,點擊這兩個按鈕都邑翻開統一個Activity,不論是誰人按鈕翻開新Activity,當這個新Activity封閉後,體系都邑挪用後面Activity的onActivityResult(int requestCode, int resultCode, Intent data)辦法。在onActivityResult()辦法假如須要曉得新Activity是由誰人按鈕翻開的,而且要做出響應的營業處置,這時候可以如許做:

@Override public void onCreate(Bundle savedInstanceState) {
....
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startActivityForResult (new Intent(MainActivity.this, NewActivity.class), 1);
}
});
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startActivityForResult (new Intent(MainActivity.this, NewActivity.class), 2);
}
});
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case 1:
//來自按鈕1的要求,作響應營業處置
case 2:
//來自按鈕2的要求,作響應營業處置
}
}
}

4 成果碼的感化

在一個Activity中,能夠會應用startActivityForResult()辦法翻開多個分歧的Activity處置分歧的營業,當這些新Activity封閉後,體系都邑挪用後面Activity的onActivityResult(int requestCode, int resultCode, Intent data)辦法。為了曉得前往的數據來自於哪一個新Activity,在onActivityResult()辦法中可以如許做(ResultActivity和NewActivity為要翻開的新Activity):

public class ResultActivity extends Activity {
.....
ResultActivity.this.setResult(1, intent);
ResultActivity.this.finish();
}
public class NewActivity extends Activity {
......
NewActivity.this.setResult(2, intent);
NewActivity.this.finish();
}
public class MainActivity extends Activity { // 在該Activity會翻開ResultActivity和NewActivity
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode){
case 1:
// ResultActivity的前往數據
case 2:
// NewActivity的前往數據
}
}
}

5 Demo源碼:

TestResultActivity.java

package mm.shandong.com.testresult;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class TestResultActivity extends AppCompatActivity {
EditText editTextBrand;
RadioGroup radioGroup;
TextView textViewXH;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_result);
editTextBrand = (EditText) findViewById(R.id.editTextBrand);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
textViewXH = (TextView) findViewById(R.id.textViewXH);
RadioButton radionButton = (RadioButton) radioGroup.getChildAt(0);
radionButton.setChecked(true);
}
///跳轉到選擇品牌界面
public void selectBrand(View view) {
Intent intent = new Intent(this, TestResultActivity1.class);
startActivityForResult(intent, 1);
}
///選摘要購置的電腦
public void selectCompute(View view) {
Intent intent = new Intent(this, TestResultActivity2.class);
String brand = editTextBrand.getText().toString();
RadioButton radionButton =
(RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
String nc = radionButton.getText().toString();
intent.putExtra("brand", brand);
intent.putExtra("nc", nc);
startActivityForResult(intent, 2);
textViewXH.setText("");
}
///activity要求前往的回調
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) { //resultCode為回傳的標志
case 1:
if (resultCode == 2) {
String brand = intent.getStringExtra("brand");
editTextBrand.setText(brand);
}
break;
case 2:
if (resultCode == 3) {
String xh = intent.getStringExtra("xh");
textViewXH.setText(xh);
}
break;
}
}
}

TestResultActivity1

package mm.shandong.com.testresult;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class TestResultActivity1 extends AppCompatActivity {
ListView listView;
String[] brands = new String[]{"聯想", "戴爾"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_result1);
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_checked, brands);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
///提交選擇的品牌成果
public void submit(View view) {
int index = listView.getCheckedItemPosition();
if (index < 0) {
Toast.makeText(this, "請選擇品牌", Toast.LENGTH_SHORT).show();
return;
}
String brand = (String) listView.getItemAtPosition(index);
int resultCode = 2;
Intent intent = getIntent();
intent.putExtra("brand", brand);
setResult(2, intent);
finish();
}
}

TestResultActivity2

package mm.shandong.com.testresult;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestResultActivity2 extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_result2);
Intent intent=getIntent();
String brand=intent.getStringExtra("brand");
String nc=intent.getStringExtra("nc");
Map map=initData();
List<String> lists= (List<String>) map.get(brand+nc);
listView= (ListView) findViewById(R.id.listView);
ArrayAdapter arrayAdapter=new ArrayAdapter(this,
android.R.layout.simple_list_item_checked,lists);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
///提交選擇的電腦型號
public void submit(View view){
int index= listView.getCheckedItemPosition();
if(index<0){
Toast.makeText(this,"請選擇型號",Toast.LENGTH_SHORT).show();
return ;
}
String xh= (String) listView.getItemAtPosition(index);
int resultCode=2;
Intent intent=getIntent();
intent.putExtra("xh",xh);
setResult(3,intent);
finish();
}
///初始化數據源
public Map initData(){
Map map=new HashMap();
List<String> lists=new ArrayList<>();
lists.add("聯想1G0001");
lists.add("聯想1G0002");
map.put("聯想1G",lists);
lists=new ArrayList<>();
lists.add("聯想2G0001");
lists.add("聯想2G0002");
map.put("聯想2G",lists);
lists=new ArrayList<>();
lists.add("戴爾1G0001");
lists.add("戴爾1G0002");
map.put("戴爾1G",lists);
lists=new ArrayList<>();
lists.add("戴爾2G0001");
lists.add("戴爾2G0002");
map.put("戴爾2G",lists);
return map;
}
}

以上所述是小編給年夜家引見的Activity實例詳解之啟動activity並前往成果,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved