安卓第二天
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
取消掉页面的标题
screenOritation———横竖屏
带有返回值的启动方式
第一个界面调用startActivityForResult(intent,requestCode)
第二个界面先调用getIntent()得到启动的Intent
关闭第二个界面前先封装数据intent.putExtra(“secondbackdata”,editText.getText().toString());
设置resultCode setResult(RESULT_OK,intent);
关闭第二个界面
第一个界面准备接受数据
重写onActivityResult(requestCode,resultCode,Intnet data)
判断resultCode
然后从data中取出数据
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//这句话的意思是去除掉屏幕最上方的状态栏(包括电池状态和时间的一栏)
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text0);
Log.d("Application000","onCreate运行");
Button btn = (Button)findViewById(R.id.button0);
//设置“开启第二个界面”按钮的点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
startActivityForResult(intent,1);//第二个值是随机设定的
}
});
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("Application000","onRestart运行");
}
@Override
protected void onStart(){
super.onStart();
Log.d("Application000","onStart运行");
}
@Override
protected void onResume(){
super.onResume();
Log.d("Application000","onResume运行");
}
@Override
protected void onPause(){
super.onPause();
Log.d("Application000","onPause运行");
}
@Override
protected void onStop(){
super.onStop();
Log.d("Application000","onStop运行");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d("Application000","onDestroy运行");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
//将Intent对象中对应的值提取,并且显示到textView上
String s =data.getStringExtra("secondBackData");
textView.setText(s);
}
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by Administrator on 2015/8/18.
*/
public class SecondActivity extends Activity {
private Button button;
private EditText editText;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
intent = getIntent();
button =(Button)findViewById(R.id.button_back);//通过ID找到返回按钮
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将文本框的数据传给intnet对象
intent.putExtra("secondBackData",editText.getText().toString());
setResult(RESULT_OK,intent);
SecondActivity.this.finish();//关闭窗口
}
});
editText = (EditText) findViewById(R.id.editText);
Log.d("Application000", "SecondActivity的onCreate运行");
}
@Override
protected void onStart(){
super.onStart();
Log.d("Application000","SecondActivity的onStart运行");
}
@Override
protected void onResume(){
super.onResume();
Log.d("Application000","SecondActivity的onResume运行");
}
@Override
protected void onPause(){
super.onPause();
Log.d("Application000","SecondActivity的onPause运行");
}
@Override
protected void onStop(){
super.onStop();
Log.d("Application000","SecondActivity的onStop运行");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d("Application000","SecondActivity的onDestroy运行");
}
}
页面编辑
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:text=""
android:layout_width="wrap_content"
android:id="@+id/text0"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运行第二个界面"
android:layout_below="@string/text_Id"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="得到返回值"
android:id="@+id/button_back" />
</LinearLayout>
运行结果:
用一个界面完成打电话,发短信,打开网页,以及隐式运行
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Intent_test000 extends Activity {
private Button button_Dial;
private Button button1_Call;
private Button button_SMS;
private Button button_Web;
private Button button_Hide;
private EditText edit_Text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_test000);
edit_Text= (EditText) findViewById(R.id.editText_hidein);
button_Hide= (Button) findViewById(R.id.button_hide);
button_Hide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.administrator.helloworld000");
intent.putExtra("edit000",edit_Text.getText().toString());
startActivity(intent);
}
});
button_Web= (Button) findViewById(R.id.button_web);
button_Web.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
button_SMS= (Button) findViewById(R.id.button_sms);
button_SMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:13140168180"));
intent.putExtra("sms_body","The SMS text");
startActivity(intent);
}
});
button_Dial= (Button)findViewById(R.id.button_dial);
button_Dial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:13140168180"));
startActivity(intent);
}
});
button1_Call= (Button)findViewById(R.id.button_call);
button1_Call.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:13140168180"));
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_intent_test000, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//这句话的意思是去除掉屏幕最上方的状态栏(包括电池状态和时间的一栏)
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text0);
intent=getIntent();
String k = intent.getStringExtra("edit000");
textView.setText(k);
Log.d("Application000","onCreate运行");
Button btn = (Button)findViewById(R.id.button0);
//设置“开启第二个界面”按钮的点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
startActivityForResult(intent,1);//第二个值是随机设定的
}
});
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("Application000","onRestart运行");
}
@Override
protected void onStart(){
super.onStart();
Log.d("Application000","onStart运行");
}
@Override
protected void onResume(){
super.onResume();
Log.d("Application000","onResume运行");
}
@Override
protected void onPause(){
super.onPause();
Log.d("Application000","onPause运行");
}
@Override
protected void onStop(){
super.onStop();
Log.d("Application000","onStop运行");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d("Application000","onDestroy运行");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
//将Intent对象中对应的值提取,并且显示到textView上
String s =data.getStringExtra("secondBackData");
textView.setText(s);
}
}
}
因为隐式运行时运行的上一个程序,所以只把不同的那段代码放上来
页面编辑:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.helloworld000.Intent_test000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨号界面"
android:id="@+id/button_dial"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打电话"
android:id="@+id/button_call"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开网页"
android:id="@+id/button_web"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="85dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信"
android:id="@+id/button_sms"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐式启动"
android:id="@+id/button_hide"
android:layout_alignTop="@+id/button_dial"
android:layout_toRightOf="@+id/button_dial"
android:layout_toEndOf="@+id/button_dial"
android:layout_marginLeft="107dp"
android:layout_marginStart="107dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_hidein"
android:layout_below="@+id/button_call"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
运行结果:
拨号界面:
拨打电话:
发送短信:
打开网页:
隐式启动: