【Android】关于 startActivityForResult 的一个小应用
短信预约 -IT技能 免费直播动态提醒
Content界面图第一个Activity (SearchContact)第二个Activity (PickContact)第三个Activity (AnotherPick) 和第二个一样其他
从一个 Activity 中打开另一个 Activity,并返回获得结果
界面图
作者:Earnest~
第一个Activity (SearchContact) activity_main.xml
MainActivity.java
package com.example.searchcontact;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.searchContact.MESSAGE";
private static final int PICK_CONTACT_REQUEST = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intent intent = getIntent();
// String phoneNumber = intent.getStringExtra(PickContact.PICK_MESSAGE);
}
public void search(View view) {
Intent intent = new Intent("searchPhoneNumber");
// intent.setAction("searchPhoneNumber");
EditText editText = (EditText)findViewById(R.id.ma_et_input);
String name = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, name);
if(intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, PICK_CONTACT_REQUEST);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {
// Cursor cursor = getContentResolver().query(data.getData(),
// new String[] {ContactsContract.Contacts.DISPLAY_NAME}, null, null, null);
// if(cursor.moveToFirst()) {
// int columnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
// String name = cursor.getString(columnIndex);
// }
TextView textView = (TextView)findViewById(R.id.ma_tv_phoneNumber);
textView.setText(data.getDataString());
}
}
}
第二个Activity (PickContact)
activity_pick_contact.xml
PickContact.java
package com.example.searchcontact;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class PickContact extends AppCompatActivity {
// public static final String PICK_MESSAGE = "com.example.pickContact.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_contact);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView)findViewById(R.id.pc_tv_empty);
textView.setText(name);
}
public void returnTOMain(View view) {
// Intent intent = new Intent(this, MainActivity.class);
EditText editText = (EditText)findViewById(R.id.pc_et_inputPhoneNumber);
String phoneNumber = editText.getText().toString();
// intent.putExtra(PICK_MESSAGE, phoneNumber);
// startActivity(intent);
Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse(phoneNumber));
// result.putExtra(PICK_MESSAGE, phoneNumber);
setResult(Activity.RESULT_OK, result);
finish();
}
}
第三个Activity (AnotherPick) 和第二个一样
activity_another_pick.xml
AnotherPick.java
package com.example.searchcontact;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class AnotherPick extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_pick);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView)findViewById(R.id.ap_tv_empty);
textView.setText(name);
}
public void returnTOMain(View view) {
EditText editText = (EditText)findViewById(R.id.ap_et_inputPhoneNumber);
String phoneNumber = editText.getText().toString();
Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse(phoneNumber));
setResult(Activity.RESULT_OK, result);
finish();
}
}
其他
AndroidManifest.xml 清单
strings.xml
SearchContact
姓名:
查询
电话号码:
请输入
您要查询的姓名
请输入电话号码
返回
空
其他的没动.
End.
Earnest~ 原创文章 93获赞 187访问量 2万+ 关注 私信 展开阅读全文作者:Earnest~
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341