举例说我想要做的一个事情是,在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Activity处理。这个时候就要用到回调函数onActivityResult()。
现在贴上一个关于onActivityResult的用法的小例子的代码:
onActivityResult.java
主要的Activity,一会会先执行此Activity,然后调用B Activity,并向其发送数据,通过回调函数来获取B传回来的值。
public class onActivityResult extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StartA();
}
public void StartA()
{
Button buttonA = (Button)findViewById(R.id.button1);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// onActivityResult.this.setContentView(R.layout.second);
// onActivityResult.this.StartB();
Intent intent = new Intent(onActivityResult.this,ClassB.class);
String passString = "Hello,this is A";
intent.putExtra("ToB", passString);
startActivityForResult(intent, 0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) { //resultCode为回传的标记,我在B中回传的是RESULT_OK
case RESULT_OK:
Bundle b=data.getExtras(); //data为B中回传的Intent
String str=b.getString("FromB");//str即为回传的值
TextView view = (TextView)findViewById(R.id.textA);
view.setText(str);
break;
default:
break;
}
}
/*public void StartB()
{
Button buttonB = (Button)findViewById(R.id.button2);
buttonB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
onActivityResult.this.setContentView(R.layout.main);
onActivityResult.this.StartA();
}
});
}*/
}
----------------------------------------------
ClassB.java
public class ClassB extends Activity{
private Intent intent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
intent = getIntent();
String result = intent.getStringExtra("ToB");
Button button = (Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ClassB.this,onActivityResult.class);
String passString = "Hello,this is B";
intent.putExtra("FromB", passString);
setResult(RESULT_OK, intent);
finish();
}
});
TextView textView = (TextView)findViewById(R.id.textB);
textView.setText(result);
}
}
-------------------------------------
风格为Theme.Dialog的Activity,即manifest里对Activity加上属性android:theme="@android:style/Theme.Dialog",
如果Activity里的所有布局是纯代码所写,不是从layout xml里生成,布局包含一个ScrollView,ScrollView里有很多TextView, EditText这类的小widget, 当用户点击其中一个EditText时,软键盘弹出来盖住了Activity的UI,Activity的UI没有自动缩小显示出scrollview的滚动条, 这是为什么呢?
这跟Activity的一个属性有关,即android:windowSoftInputMode:
这里设置其为 android:windowSoftInputMode=“stateHidden|adjustResize”即可解决上述我们的问题!
具体是什么原因,看以下SDK文档解释就明白了:
How the main window of the activity interacts with the window containing the on-screen soft keyboard. The setting for this attribute affects two things:
- The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
- The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.
The setting must be one of the values listed in the following table, or a combination of one "state... " value plus one "adjust... " value. Setting multiple values in either group — multiple "state... " values, for example — has undefined results. Individual values are separated by a vertical bar (| ). For example:
<activity android:windowSoftInputMode = "stateVisible|adjustResize" . . . >
Values set here (other than "stateUnspecified " and "adjustUnspecified ") override values set in the theme.
Value Description "stateUnspecified " The state of the soft keyboard (whether it is hidden or visible) is not specified. The system will choose an appropriate state or rely on the setting in the theme.
This is the default setting for the behavior of the soft keyboard.
"stateUnchanged " The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore. "stateHidden " The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity. "stateAlwaysHidden " The soft keyboard is always hidden when the activity's main window has input focus. "stateVisible " The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window). "stateAlwaysVisible " The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity. "adjustUnspecified " It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the currentfocus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area.This is the default setting for the behavior of the main window.
"adjustResize " The activity's main window is always resized to make room for the soft keyboard on screen. "adjustPan " The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
具体步骤:
首先,我们要启动两个android模拟器。只有这样,才能模拟效果。
1.第一个模拟器,我们通过eclipse启动。
具体方法是:打开你的eclipse,随便运行一个项目。Eclipse会帮你启动我们的第一个android模拟器。ID为5554.
2.第二个模拟器,我们通过DOS命令启动。具体方法:“开始”-“运行”-“CMD打开一个命令行窗口。然后cd 命令到你的android SDK目录的tools文件夹下面,输入“emulator -data foo”,回车。稍等片刻,系统即可帮你启动又一个新的android模拟器。ID为5556.这2个模拟器的ID 就是对应的模拟器的电话号码
3.于是两个就有了2个模拟器的ID了,这里的ID号相当于真机上的手机号码。收发短信和接打电话全靠他们。
好了,两个全新的模拟器我们已经启动完毕。
现在来测试接打电话和收发短信。
怎么测试呢。通过id号,进行和手机打电话和发短信同样的操作,即可。
注意 : 如果发现打不通 或者发不了短信 查看一下 你当前模拟器的那个网络信号状态
如果是飞行模式 是实现不了的哦。