Activity之间相互跳转和传递数据(包括Button样式自定义、Check样式自定义和Activity添加Menu)

1.1 MainActivity.java代码如下,代码里面有大部分有注释说明了,应该还是很清楚的,欢迎大佬指教,小小白的同志们如果有什么不清楚的地方可以给我留言哦:
  1. import android.content.Intent;  
  2. import android.net.Uri;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.CheckBox;  
  11. import android.widget.CompoundButton;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {  
  16.   
  17.     private String string = “Hello World!”;  
  18.     private TextView mtv;  
  19.     private Button mbt;  
  20.     private CheckBox mche;  
  21.     private TextView mtv2;  
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate ( savedInstanceState );  
  25.         setContentView ( R.layout.activity_main );  
  26.         mtv = findViewById ( R.id.mtv);  
  27.         mbt = findViewById ( R.id.mbt );  
  28.         mche = findViewById ( R.id.mche );  
  29.         mtv2 = findViewById ( R.id.mtv2 );  
  30.         mtv.setOnClickListener ( new MytvOnClickLister () );  
  31.         mbt.setOnClickListener ( new MybtOnClickLister () );  
  32.         mche.setOnCheckedChangeListener(this);  
  33.     }  
  34.     @Override  
  35.     //Check的点击执行函数  
  36.     public void onCheckedChanged(CompoundButton compoundButton, boolean b) {  
  37.         String desc = String.format(”我%s”,b?“被选中了”:“没被选中”);  
  38.         compoundButton.setText(desc);  
  39.     }  
  40.     //TextView点击事件  
  41.     class MytvOnClickLister implements View.OnClickListener {  
  42.         @Override  
  43.         public void onClick(View view) {  
  44.             if (view.getId () == R.id.mtv) {  
  45.                 Toast.makeText ( MainActivity.this“TextView点击”, Toast.LENGTH_SHORT ).show ();  
  46.                 mtv.setText ( ”被玩坏了” );  
  47.                 mbt.setText ( ”住手” );  
  48.                 mche.setChecked ( true );  
  49.                 mche.setText ( ”我被选中了” );  
  50.                 //下面是一个延时程序,延时2s后执行ruu()函数  
  51.                 new Handler ().postDelayed ( new Runnable () {  
  52.                     public void run() {  
  53.                         mtv.setText (string);  
  54.                     }  
  55.                 }, 2000 );  
  56.             }  
  57.         }  
  58.     }  
  59.     //Buton点击事件  
  60.     class MybtOnClickLister implements View.OnClickListener {  
  61.         @Override  
  62.         public void onClick(View view) {  
  63.             //按钮修改Check的选中状态  
  64.             /*if (view.getId () == R.id.mbt) { 
  65.                 Toast.makeText ( MainActivity.this, “button点击”, Toast.LENGTH_SHORT ).show (); 
  66.                 mche.setChecked ( false ); 
  67.                 mche.setText ( ”我没被选中” ); 
  68.             }*/  
  69.   
  70.             //隐式Intent的用法:打开一个网页  
  71.            /* Intent intent = new Intent ( Intent.ACTION_VIEW); 
  72.             intent.setData ( Uri.parse ( “http://www.baidu.com” ) ); 
  73.             startActivity ( intent );*/  
  74.   
  75.             //隐式Intent的用法:打开电话  
  76.             /*Intent intent = new Intent ( Intent.ACTION_DIAL); 
  77.             intent.setData ( Uri.parse ( “tel:10086” ) ); 
  78.             startActivity ( intent );*/  
  79.   
  80.             //传递数据给SecondActivity  
  81.            /* String s = ”我是被传递的数据”; 
  82.             Intent intent = new Intent ( MainActivity.this,SecondActivity.class ); 
  83.             intent.putExtra ( “extra_data”,s ); 
  84.             startActivity ( intent );*/  
  85.            //接收从SecondActivity返回的数据  
  86.             Intent intent = new Intent ( MainActivity.this,SecondActivity.class );  
  87.             startActivityForResult ( intent,1 );  
  88.         }  
  89.     }  
  90.     //接收其他Activity传过来的数据第一个参数是判断是那个Activity传过来的,第二个参数是判断结果处理是否成功  
  91.     protected void onActivityResult(int requestCode,int resultCode,Intent data){  
  92.         switch (requestCode){  
  93.             case 1:  
  94.                 if (resultCode == RESULT_OK){  
  95.                     mtv2.setText ( ”返回的随机数”+data.getStringExtra ( “return_data” ) );  
  96.                 }  
  97.                 break;  
  98.             default:  
  99.         }  
  100.     }  
  101.     //给Activity添加菜单  
  102.     public boolean onCreateOptionsMenu(Menu menu){  
  103.         getMenuInflater ().inflate ( R.menu.main,menu);  
  104.         return true;  
  105.     }  
  106.     //实现菜单功能  
  107.     public boolean onOptionsItemSelected(MenuItem item){  
  108.         switch (item.getItemId ()){  
  109.             case R.id.add_menu1:  
  110.                 Toast.makeText ( this,“切换不了,放弃吧!”,Toast.LENGTH_SHORT ).show ();  
  111.                 break;  
  112.             case R.id.add_menu2:  
  113.                 finish ();  
  114.                 break;  
  115.             default:  
  116.         }  
  117.         return true;  
  118.     }  
  119. }  
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private String string = "Hello World!";
    private TextView mtv;
    private Button mbt;
    private CheckBox mche;
    private TextView mtv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        mtv = findViewById ( R.id.mtv);
        mbt = findViewById ( R.id.mbt );
        mche = findViewById ( R.id.mche );
        mtv2 = findViewById ( R.id.mtv2 );
        mtv.setOnClickListener ( new MytvOnClickLister () );
        mbt.setOnClickListener ( new MybtOnClickLister () );
        mche.setOnCheckedChangeListener(this);
    }
    @Override
    //Check的点击执行函数
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String desc = String.format("我%s",b?"被选中了":"没被选中");
        compoundButton.setText(desc);
    }
    //TextView点击事件
    class MytvOnClickLister implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (view.getId () == R.id.mtv) {
                Toast.makeText ( MainActivity.this, "TextView点击", Toast.LENGTH_SHORT ).show ();
                mtv.setText ( "被玩坏了" );
                mbt.setText ( "住手" );
                mche.setChecked ( true );
                mche.setText ( "我被选中了" );
                //下面是一个延时程序,延时2s后执行ruu()函数
                new Handler ().postDelayed ( new Runnable () {
                    public void run() {
                        mtv.setText (string);
                    }
                }, 2000 );
            }
        }
    }
    //Buton点击事件
    class MybtOnClickLister implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            //按钮修改Check的选中状态
            /*if (view.getId () == R.id.mbt) {
                Toast.makeText ( MainActivity.this, "button点击", Toast.LENGTH_SHORT ).show ();
                mche.setChecked ( false );
                mche.setText ( "我没被选中" );
            }*/

            //隐式Intent的用法:打开一个网页
           /* Intent intent = new Intent ( Intent.ACTION_VIEW);
            intent.setData ( Uri.parse ( "http://www.baidu.com" ) );
            startActivity ( intent );*/

            //隐式Intent的用法:打开电话
            /*Intent intent = new Intent ( Intent.ACTION_DIAL);
            intent.setData ( Uri.parse ( "tel:10086" ) );
            startActivity ( intent );*/

            //传递数据给SecondActivity
           /* String s = "我是被传递的数据";
            Intent intent = new Intent ( MainActivity.this,SecondActivity.class );
            intent.putExtra ( "extra_data",s );
            startActivity ( intent );*/
           //接收从SecondActivity返回的数据
            Intent intent = new Intent ( MainActivity.this,SecondActivity.class );
            startActivityForResult ( intent,1 );
        }
    }
    //接收其他Activity传过来的数据第一个参数是判断是那个Activity传过来的,第二个参数是判断结果处理是否成功
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        switch (requestCode){
            case 1:
                if (resultCode == RESULT_OK){
                    mtv2.setText ( "返回的随机数"+data.getStringExtra ( "return_data" ) );
                }
                break;
            default:
        }
    }
    //给Activity添加菜单
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater ().inflate ( R.menu.main,menu);
        return true;
    }
    //实现菜单功能
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId ()){
            case R.id.add_menu1:
                Toast.makeText ( this,"切换不了,放弃吧!",Toast.LENGTH_SHORT ).show ();
                break;
            case R.id.add_menu2:
                finish ();
                break;
            default:
        }
        return true;
    }
}

1.2 MainActivity布局代码:

  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=“com.example.lyp.myapplication.MainActivity”>  
  7.   
  8.     <TextView  
  9.         android:id=“@+id/mtv”  
  10.         android:layout_width=“wrap_content”  
  11.         android:layout_height=“wrap_content”  
  12.         android:text=“Hello World!”  
  13.         android:background=“#c79898”  
  14.         android:textSize=“30sp”  
  15.         android:layout_marginTop=“20dp”  
  16.         android:layout_centerHorizontal=“true”  
  17.         />  
  18.     <Button  
  19.         android:id=“@+id/mbt”  
  20.         android:layout_width=“wrap_content”  
  21.         android:layout_height=“wrap_content”  
  22.         android:layout_below=“@+id/mtv”  
  23.         android:text=“button”  
  24.         android:background=“@drawable/button”  
  25.         android:layout_marginTop=“10dp”  
  26.         android:textAllCaps=“false”  
  27.         android:textSize=“25sp”  
  28.         android:layout_centerHorizontal=“true”  
  29.   
  30.         />  
  31.     <CheckBox  
  32.         android:id=“@+id/mche”  
  33.         android:layout_width=“wrap_content”  
  34.         android:layout_height=“wrap_content”  
  35.         android:layout_below=“@+id/mbt”  
  36.         android:layout_marginTop=“10dp”  
  37.         android:textColor=“#ff0008”  
  38.         android:textSize=“15sp”  
  39.         android:button=“@drawable/check”  
  40.         android:text=“我没被选中!”  
  41.         android:layout_marginLeft=“50dp”  
  42.         android:layout_centerHorizontal=“true”  
  43.         />  
  44.     <TextView  
  45.         android:id=“@+id/mtv2”  
  46.         android:layout_width=“wrap_content”  
  47.         android:layout_height=“wrap_content”  
  48.         android:text=“别管我”  
  49.         android:background=“#c79898”  
  50.         android:textSize=“30sp”  
  51.         android:layout_marginTop=“10dp”  
  52.         android:layout_centerHorizontal=“true”  
  53.         android:layout_below=“@+id/mche”  
  54.         />  
  55. </RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.lyp.myapplication.MainActivity">

    <TextView
        android:id="@+id/mtv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:background="#c79898"
        android:textSize="30sp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        />
    <Button
        android:id="@+id/mbt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mtv"
        android:text="button"
        android:background="@drawable/button"
        android:layout_marginTop="10dp"
        android:textAllCaps="false"
        android:textSize="25sp"
        android:layout_centerHorizontal="true"

        />
    <CheckBox
        android:id="@+id/mche"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mbt"
        android:layout_marginTop="10dp"
        android:textColor="#ff0008"
        android:textSize="15sp"
        android:button="@drawable/check"
        android:text="我没被选中!"
        android:layout_marginLeft="50dp"
        android:layout_centerHorizontal="true"
        />
    <TextView
        android:id="@+id/mtv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="别管我"
        android:background="#c79898"
        android:textSize="30sp"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/mche"
        />
</RelativeLayout>

1.3 Button样式自定义(其实就是按下后会变个样子):

分别是:button、pressed和unpressed文件

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <selector xmlns:android=“http://schemas.android.com/apk/res/android”>  
  3.     <item android:state_pressed=“true” android:drawable=“@drawable/pressed”/>  
  4.     <item android:drawable=“@drawable/unpressed”/>  
  5. </selector>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/pressed"/>
    <item android:drawable="@drawable/unpressed"/>
</selector>
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <shape xmlns:android=“http://schemas.android.com/apk/res/android”>  
  3.     <corners  
  4.         android:radius=“5dp”  
  5.         />  
  6.     <stroke  
  7.         android:color=“@color/colorPrimary”  
  8.         android:width=“3dp”  
  9.         />  
  10.     <solid  
  11.         android:color=“@color/colorAccent”/>  
  12. </shape>  
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:radius="5dp"
        />
    <stroke
        android:color="@color/colorPrimary"
        android:width="3dp"
        />
    <solid
        android:color="@color/colorAccent"/>
</shape>
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <shape xmlns:android=“http://schemas.android.com/apk/res/android”>  
  3.     <corners  
  4.         android:radius=“5dp”  
  5.         />  
  6.     <stroke  
  7.         android:color=“@color/colorAccent”  
  8.         android:width=“3dp”  
  9.         />  
  10.     <solid  
  11.         android:color=“@color/colorPrimary”/>  
  12. </shape>  
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:radius="5dp"
        />
    <stroke
        android:color="@color/colorAccent"
        android:width="3dp"
        />
    <solid
        android:color="@color/colorPrimary"/>
</shape>

1.4 Check样式自定义:

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <selector xmlns:android=“http://schemas.android.com/apk/res/android”>  
  3.     <item android:state_checked=“true” android:drawable=“@drawable/check_choose”/>  
  4.     <item android:drawable=“@drawable/check_unchoose”/>  
  5. </selector>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/check_choose"/>
    <item android:drawable="@drawable/check_unchoose"/>
</selector>

这是自定义Check是用到的选中和未选中的素材:

1.5 Mainactivity截图:


说明:其中第一个控件没什么作用,我在其中就学到了和延时函数的使用,Button的作用是跳转到其他的Activity中,接着就是一个Check复选框按钮,我在TextView和Button的点击事件中的设置了Check的选中状态,所以当点击他们的时候Check的状态会变化。不过后面奖Buton设置成跳转到SecondActivity了。最后一个也是一个TextView,他的作用主要是显示从其他的Activity传过来的数据。这里我设置的是传过来一个随机数(Math.random()函数产生0到1的随机数。返回值是double型)。所以每次传的数据都是不一样的。

2.1 SecondActivity.java代码:

  1. import android.content.Intent;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.widget.TextView;  
  7.   
  8. public class SecondActivity extends AppCompatActivity {  
  9.   
  10.     private TextView second_mtv;  
  11.     private Button  second_mbt;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate ( savedInstanceState );  
  16.         setContentView ( R.layout.activity_second );  
  17.         second_mtv = findViewById ( R.id.second_mtv );  
  18.         second_mbt = findViewById ( R.id.second_mbt );  
  19.   
  20.         //接收来自MainActivity的数据  
  21.         /*Intent intent = getIntent (); 
  22.         second_mtv.setText ( intent.getStringExtra ( “extra_data” ) ); 
  23.         second_mtv.setTextSize ( 30 );*/  
  24.   
  25.         //返回数据到MainActivity  
  26.         second_mbt.setOnClickListener ( new View.OnClickListener (){  
  27.             @Override  
  28.             public void onClick(View view){  
  29.                 int i = (int)( Math.random()*100);//产生一个随机数  
  30.                 Intent intent = new Intent (  );  
  31.                 intent.putExtra ( ”return_data”,Double.toString ( i ));//将数据传输过去  
  32.                 setResult ( RESULT_OK,intent );  
  33.                 finish ();//销毁当前Activity  
  34.             }  
  35.         } );  
  36.     }  
  37. }  
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    private TextView second_mtv;
    private Button  second_mbt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_second );
        second_mtv = findViewById ( R.id.second_mtv );
        second_mbt = findViewById ( R.id.second_mbt );

        //接收来自MainActivity的数据
        /*Intent intent = getIntent ();
        second_mtv.setText ( intent.getStringExtra ( "extra_data" ) );
        second_mtv.setTextSize ( 30 );*/

        //返回数据到MainActivity
        second_mbt.setOnClickListener ( new View.OnClickListener (){
            @Override
            public void onClick(View view){
                int i = (int)( Math.random()*100);//产生一个随机数
                Intent intent = new Intent (  );
                intent.putExtra ( "return_data",Double.toString ( i ));//将数据传输过去
                setResult ( RESULT_OK,intent );
                finish ();//销毁当前Activity
            }
        } );
    }
}

2.2 SecondActivity布局文件:

  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.     android:gravity=“center_horizontal”  
  7.     tools:context=“com.example.lyp.myapplication.SecondActivity”>  
  8.   
  9.     <TextView  
  10.         android:id=“@+id/second_mtv”  
  11.         android:layout_width=“wrap_content”  
  12.         android:layout_height=“wrap_content”  
  13.         android:hint=“显示传递过来的数据”  
  14.         android:layout_marginTop=“50dp”  
  15.         />  
  16.     <Button  
  17.         android:id=“@+id/second_mbt”  
  18.         android:layout_width=“wrap_content”  
  19.         android:layout_height=“wrap_content”  
  20.         android:layout_below=“@+id/second_mtv”  
  21.         android:layout_marginTop=“20dp”  
  22.         android:background=“@drawable/button”  
  23.         android:text=“button”  
  24.         android:textAllCaps=“false”  
  25.         android:textSize=“20sp”  
  26.         />  
  27. </RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center_horizontal"
    tools:context="com.example.lyp.myapplication.SecondActivity">

    <TextView
        android:id="@+id/second_mtv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="显示传递过来的数据"
        android:layout_marginTop="50dp"
        />
    <Button
        android:id="@+id/second_mbt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/second_mtv"
        android:layout_marginTop="20dp"
        android:background="@drawable/button"
        android:text="button"
        android:textAllCaps="false"
        android:textSize="20sp"
        />
</RelativeLayout>

源代码下载

            </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值