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时不要着急,每一行代码,每一个字母都要认真的敲,不要出错,养成良好的编码习惯,学会自己找错误,将遇到的错误总结起来,方便以后再次查阅解决。耐心耐心再耐心相关文章推荐
- Android Camera拍照 压缩
- android 各种对话框 AlertDialog
- android项目创建xml和存储xml文件
- android从本机选取图片
- Android 将.sql格式文件转换为.db格式数据库文件
- Android基础布局+Activity启动+Intent
- android中的Activity和Intent
- Android之玩转选项卡(TabHost、TabWidget、FrameLayout)
- Android 中对于图片的内存优化方法
- android 处理图片之--bitmap处理
- Android 入门(1)
- Android_ConnectivityManager连接管理
- Android 使用外部db数据库文件
- Android动态设置actionBar
- Android之如何改变焦点状态【EditText】
- Android的全屏、标题栏、横竖屏设置
- Android Togglebutton 默认背景被放大
- android4.4修改设置中默认定位模式
- ListView、ScrollView、GridView滑动到顶端或底部的阴影效果去掉
- android studio中文乱码的解决方法【转】