您的位置:首页 > 移动开发 > Android开发

Android 笔记2

2015-08-19 20:16 344 查看

目录

活动的两种启动方式

下一个活动返回值给上一个活动

package com.example.linj.myfirstapplication;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
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.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FirstActivity extends Activity {

private TextView textView;
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.first_layout);
textView = (TextView) findViewById(R.id.text_show);
intent = getIntent();
textView.setText(intent.getStringExtra("BACK"));
Button button = (Button) findViewById(R.id.button_first);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.qq.com"));
startActivity(intent);
//                startActivityForResult(intent, 1);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
String s = data.getStringExtra("data_return");
textView.setText(s);
}
}
}
//被启动的活动
package com.example.linj.myfirstapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

/**
* Created by linj on 2015/8/17.
*/
public class SecondActivity extends Activity {

private Intent intent;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
intent = getIntent();
editText = (EditText) findViewById(R.id.edit);
Button button2 = (Button) findViewById(R.id.button_back);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("data_return", editText.getText().toString());
setResult(RESULT_OK, intent);
SecondActivity.this.finish();
}
});
}
}


Intent

Intent的各种用法

活动,新建一些按钮来实现Intent的各种显示调用,隐式调用,传值

package com.example.linj.myfirstapplication;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

/**
* Created by Administrator on 2015/8/17.
*/
public class ThirdActivity extends Activity {

private Button mButton_dial;
private Button mButton_call;
private Button mButton_net;
private Button mButton_sms;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.third_layout);
mButton_dial = (Button) findViewById(R.id.button_dial);
mButton_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:10086"));
startActivity(intent);
}
});

mButton_call = (Button) findViewById(R.id.button_call);
mButton_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:10086"));
startActivity(intent);
}
});

mButton_sms = (Button) findViewById(R.id.button_sms);
mButton_sms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:10086"));
intent.putExtra("sms_body", "还钱");
startActivity(intent);
}
});

mButton_net = (Button) findViewById(R.id.button_net);
mButton_net.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.qq.com"));
startActivity(intent);
}
});

editText = (EditText) findViewById(R.id.edit_back);
Button btnbfirst = (Button) findViewById(R.id.button_bfirst);
btnbfirst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.linj.myfirstapplication.MY_ACTION");
intent.putExtra("BACK", editText.getText().toString());
startActivity(intent);
}
});
}
}


布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Button
android:id="@+id/button_dial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拨号"
/>

<Button
android:id="@+id/button_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="直接拨打电话"
/>

<Button
android:id="@+id/button_net"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上网"
/>

<Button
android:id="@+id/button_sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发短信"
/>

<EditText
android:id="@+id/edit_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button_bfirst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回第一个界面"
/>
</LinearLayout>


总结

在初学Android时不要着急,每一行代码,每一个字母都要认真的敲,不要出错,养成良好的编码习惯,学会自己找错误,将遇到的错误总结起来,方便以后再次查阅解决。耐心耐心再耐心
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: