• 0108 Activity的生命周期(二)+显示文本的几种方法+调用发送短信(Intent)


    0108

    Task概念

    栈 存放Activity  的数据结构

    要以对话框形式出现

    在Manifest.xml文件中的<activity>里加入

    android:theme="@android:style/Theme.Dialog"

    ------------------------------------------------------------

    显示文本的几种方法

    第一种

    symbol.setText("乘以");

    calculate.setText("计算");

    第二种

    设置要显示的值,但是这种方法显示语言会写死,不适合国际化平台

    android中的strings.xml

    <string name ="symbol">乘以</string>

    <string name ="calculate">计算</string>

    在Activity控件中可以这么写以显示

    symbol.setText(R.string.symbol);

    calculate.setText(R.string.calculate);真正的值在strings.xml这个文件中

    可以在android平台上放上不同语言的 strings.xml 比如日文或者英文版本的strings.xml

    所以推荐把字符窜放在strings.xml中

    第三种

    android中的strings.xml

     <string name="back_button">返回</string>

    在layout部署文件中

     android:text="@string/back_button"

    不需要再Activity添加文件

    --------------------------------------------------------------

    调用发送短信

    -----------------------------------------------------------------

    class sendSMSListener implements OnClickListener{

                      

                       @Override

                       public void onClick(View v) {

                                // TODO Auto-generated method stub

                                Uri uri=Uri.parse("smsto:18796014917");//Creates a uri which parses the given encoded URI string

                                Intent intent=new Intent(Intent.ACTION_SENDTO,uri);//Activity Action: Send a message to someone specified by the data.

                                intent.putExtra("sms_body", "The  SMS text");

                                startActivity(intent);

                               

                               

                       }

    -------------------------------------------------------------------                      

     Activity02

     1 package com.example.mars_activity;
     2 
     3 import android.os.Bundle;
     4 import android.app.Activity;
     5 import android.content.Intent;
     6 import android.view.Menu;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 
    11 public class Activity02 extends Activity {
    12     private Button myButton=null;
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         setContentView(R.layout.activity_activity02);
    18         
    19      myButton =(Button)findViewById(R.id.myButton);
    20      myButton.setOnClickListener(new MyButtonListener());//这样就把MyButtonListener捆绑在myButton这个按钮上了
    21         
    22     }
    23 
    24     @Override
    25     public boolean onCreateOptionsMenu(Menu menu) {
    26         // Inflate the menu; this adds items to the action bar if it is present.
    27         getMenuInflater().inflate(R.menu.activity_activity02, menu);
    28         return true;
    29     }
    30     class MyButtonListener implements OnClickListener//这是一个内部类 实现OnClickListener这个接口
    31     {//此为内部类,是一个监听器
    32 
    33         @Override
    34         public void onClick(View v) {
    35             // TODO Auto-generated method stub
    36             //生成一个Intent对象
    37             Intent intent =new Intent();
    38         intent.setClass(Activity02.this, OtherActivity.class);//从一个activity跳转到另一个activity
    39             //intent相当于一个请求 ,请求第二个参数
    40         Activity02.this.startActivity(intent);//把intent传进去,startService是Activity的一个方法
    41             //这样就完成监听器的编写,然后要把这个监听器捆绑在这样按钮上
    42         }//实现这个接口都要复写onClick()这个方法
    43         
    44     }
    45 
    46 }

     OtherActivity.java

     1 package com.example.mars_activity;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.net.Uri;
     6 import android.os.Bundle;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 import android.widget.TextView;
    11 
    12 public class OtherActivity extends Activity {
    13    private TextView myTextView=null;
    14    private Button myButton;
    15    private Button sendSMS;
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         // TODO Auto-generated method stub
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.others);//让Activity使用other.xml 的布局layout
    21          myTextView=(TextView)findViewById(R.id.myTextView);
    22         myTextView.setText(R.string.other);//在value-string里面添加<string name="other">OtherActivity</string> R中会自动生成
    23     myButton=(Button)findViewById(R.id.myButton);
    24     myButton.setOnClickListener(new ButtonListener());
    25     sendSMS=(Button)findViewById(R.id.sendSMS);
    26     sendSMS.setOnClickListener(new sendSMSListener());
    27     sendSMS.setText("send SMS");
    28     
    29     }//source-override implement/method  直接出来
    30     
    31     class ButtonListener implements OnClickListener
    32     {
    33 
    34         @Override
    35         public void onClick(View arg0) {
    36             // TODO Auto-generated method stub
    37             Intent intent=new Intent();
    38             intent.setClass(OtherActivity.this, Activity02.class);
    39             OtherActivity.this.startActivity(intent);
    40             
    41             
    42         }
    43         
    44     }
    45     class sendSMSListener implements OnClickListener{
    46         
    47 
    48         @Override
    49         public void onClick(View v) {
    50             // TODO Auto-generated method stub
    51             Uri uri=Uri.parse("smsto:400-400-123");//Creates a uri which parses the given encoded URI string
    52             //将要执行的命令写在uri中
    53             Intent intent=new Intent(Intent.ACTION_SENDTO,uri);//Activity Action: Send a message to someone specified by the data. 
    54             //将uri解析为一个发送命令动作的请求
    55             intent.putExtra("sms_body", "The  SMS text");
    56             //执行该请求,给短信体
    57             startActivity(intent);
    58             
    59             
    60         }//执行发送短信
    61     }
    62     
    63     
    64 
    65 }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     tools:context=".Activity02" >
     7 
     8     
     9     <Button
    10         android:id="@+id/myButton"
    11         android:layout_width="fill_parent"
    12         android:layout_height="wrap_content"
    13         />
    14 
    15 </RelativeLayout>
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6     <TextView
     7         android:id="@+id/myTextView"
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         />
    11     <Button
    12         android:id="@+id/myButton"
    13         android:layout_width="fill_parent"
    14         android:layout_height="wrap_content"
    15         android:text="@string/back_button"
    16         />
    17     <Button
    18         android:id="@+id/sendSMS"
    19          android:layout_width="fill_parent"
    20         android:layout_height="wrap_content"
    21         />
    22 
    23 </LinearLayout>

    AndroidManifest.xml 添加

    1  <activity 
    2             android:name="com.example.mars_activity.OtherActivity"
    3             android:label="@string/other"
    4             android:theme="@android:style/Theme.Dialog"
    5            />
  • 相关阅读:
    Vue v-if v-for v-bind v-on
    Vue v-bind的使用
    Vue绑定事件
    vue绑定内联样式
    Vue简单使用
    js实现UTC时间转为北京时间,时间戳转为时间
    setTimeOut函数传参数
    直播聊天室,点亮效果,jquery实现
    聊天室自动滚动效果实现
    融云rongCloud聊天室的使用
  • 原文地址:https://www.cnblogs.com/kyxyes/p/2883692.html
Copyright © 2020-2023  润新知