08-19 Activity启动 全屏,横、竖屏 FrameLayout TableLayout Intent Extra传递数据

Activity启动

//****直接启动****
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        intent=getIntent();
        textView= (TextView) findViewById(R.id.textView);
        textView.setText(intent.getStringExtra(Config.INTENT_TO));
        Button btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,Second_Activity.class);
                startActivityForResult(intent);
            }
        });
    }
//****带有返回值的启动****    Activity跳转  
//**MainActivity**
public class MainActivity extends Activity {
    private TextView textView;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        intent=getIntent();
        textView= (TextView) findViewById(R.id.textView);
        textView.setText(intent.getStringExtra(Config.INTENT_TO));
        Button btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,Second_Activity.class);
                startActivityForResult(intent,12);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            String s=data.getStringExtra("sss");
            textView.setText(s);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("myApplication", "onStart ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("myApplication", "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("myApplication", "onResume ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("myApplication", "onPause ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("myApplication", "onStop ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("myApplication", "onDestroy ");
    }
}

//**activity_main.xml**
<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="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动第二个界面"
        android:id="@+id/button"
        android:layout_margin="@dimen/activity_horizontal_margin"
        />
</RelativeLayout>
![这里写图片描述](http://img.blog.csdn.net/20150819195712603)

//**Second_Activity**
public class Second_Activity  extends Activity{
    private EditText editText;
    private Button button;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_second);
        editText= (EditText) findViewById(R.id.editText);
//        intent=getIntent();
        button= (Button) findViewById(R.id.button_back);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.putExtra("sss",editText.getText().toString());
                setResult(RESULT_OK,intent);
                Second_Activity.this.finish();
            }
        });

        Log.d("second_activity", "onCreate ");
    }



    @Override
    protected void onStart() {
        super.onStart();
        Log.d("second_activity", "onStart ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("second_activity", "onResume ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("second_activity", "onPause ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("second_activity", "onStop ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("second_activity", "onDestroy ");
    }
}

//**activity_second**
<?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:weightSum="1">

    <TextView android:text="第二个界面"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"/>
    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="返回结果"
        android:id="@+id/button_back"/>

</LinearLayout>
![这里写图片描述](http://img.blog.csdn.net/20150819200001443)

全屏、非全屏,横屏、竖屏

//******全屏、非全屏******
   //**MainActivity**
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_main);
        intent=getIntent();
        textView= (TextView) findViewById(R.id.textView);
        textView.setText(intent.getStringExtra(Config.INTENT_TO));
        Button btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,Second_Activity.class);
                startActivityForResult(intent,12);
            }
        });
    }
//**AndroidManifest.xml**
<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">//全屏
        <intent-filter>
            <action android:name="com.example.administrator.myappplication.MainActivity"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

    </activity>    
![全屏](http://img.blog.csdn.net/20150819201216930)
![非全屏](http://img.blog.csdn.net/20150819201334453)
//******竖屏、横屏******
  //**AndroidManifest.xml**
   <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="portrait"//竖屏
        android:screenOrientation="landscape">//横屏
        <intent-filter>
            <action android:name="com.example.administrator.myappplication.MainActivity"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

    </activity>
    ![横屏](http://img.blog.csdn.net/20150819202202580)
    ![竖屏](http://img.blog.csdn.net/20150819202355427)

FrameLayout的属性

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

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#ff0000"
       android:visibility="visible" //可见
       android:visibility="invisible"//不可见,但还占据位置
       android:visibility="gone"/> //相当于消除这个按钮
    <Button
        android:layout_width="100dp"
        android:layout_height="100dp" />
</FrameLayout>
![visible](http://img.blog.csdn.net/20150819202810153)
![invisible](http://img.blog.csdn.net/20150819202902473)
![gone](http://img.blog.csdn.net/20150819202955374)

TableLayout的属性

/**
android:stretchColumns 设置可伸展的列,自行沾满剩余空间

android:shrinkColumns  设置可收缩的列,自行调整大小

android:collapseColumns 设置要隐藏的列。
**/
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,1,2"

    android:shrinkColumns="1">
   <TableRow>
       <Button android:text="button1"/>
       <Button android:text="button2"/>
       <Button android:text="button3"/>
   </TableRow>
    <TableRow>
        <Button android:text="button1"/>
        <Button android:text="button2222222222222222222"/>
        <Button android:text="button3"/>
    </TableRow>
    <TableRow>
        <Button android:text="button1"/>
        <Button android:text="button2"/>
        <Button android:text="button3"/>
    </TableRow>
</TableLayout>
![这里写图片描述](http://img.blog.csdn.net/20150819204330132)

Intent

//**ActionActivity**
public class ActionActivity extends Activity{
    private Button mButtonDIAL;
    private Button mButtonCALL;
    private Button mBtnWEB;
    private Button mBtnSMS;
    private Button mBtnDIAO;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.action);

        //打电话  需要在AndroidManifest中添加权限
        /*
        <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
        */
        mButtonDIAL= (Button) findViewById(R.id.button_DIAL);
        mButtonDIAL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:18093193992"));
                startActivity(intent);
            }
        });

        //直接打电话
        mButtonCALL= (Button) findViewById(R.id.button_CALL);
        mButtonCALL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:18093193992"));
                startActivity(intent);
            }
        });

        //发短信
        mBtnSMS= (Button) findViewById(R.id.button_SMS);
        mBtnSMS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("smsto;"));
                intent.putExtra("sms_body","The SMS Text");
                startActivity(intent);
            }
        });

        //打开网页
        mBtnWEB= (Button) findViewById(R.id.button_WEB);
        mBtnWEB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });

       //隐式调用
        mBtnDIAO= (Button) findViewById(R.id.button_diao);
        editText= (EditText) findViewById(R.id.edittext);
        mBtnDIAO.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("com.example.administrator.myappplication.MainActivity");
                String value=editText.getText().toString();
                intent.putExtra(Config.INTENT_TO,value);
                startActivity(intent);
            }
        });
    }
}


//**action_layout**
<?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">

    <EditText
        android:layout_width="wrap_content"
        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_DIAL" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="直接打电话"
        android:id="@+id/button_CALL" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发短信"
        android:id="@+id/button_SMS"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开网页"
        android:id="@+id/button_WEB"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隐式启动"
        android:id="@+id/button_diao"/>
</LinearLayout>
//**AndroidManifest**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.myapplication" >
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="portrait">
        //隐式调用,跳转到哪个页面,intent-filter写在哪个页面
        <intent-filter>
            <action android:name="com.example.administrator.myappplication.MainActivity"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

    <activity android:name=".Second_Activity">
    </activity>

    <activity android:name=".ActionActivity">
    </activity>
</application>
</manifest>

![这里写图片描述](http://img.blog.csdn.net/20150819205317393)

Extra传递数据

//**发出信息的页面**
public class Second_Activity  extends Activity{
    private EditText editText;
    private Button button;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_second);
        //设置editText 发送消息的输入框
        editText= (EditText) findViewById(R.id.editText);
//        intent=getIntent();
        button= (Button) findViewById(R.id.button_back);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();

//封装消息
                intent.putExtra("sss",editText.getText().toString());
                setResult(RESULT_OK,intent);
                Second_Activity.this.finish();
            }
        });

        Log.d("second_activity", "onCreate ");
    }



    @Override
    protected void onStart() {
        super.onStart();
        Log.d("second_activity", "onStart ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("second_activity", "onResume ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("second_activity", "onPause ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("second_activity", "onStop ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("second_activity", "onDestroy ");
    }
}


//**得到信息的页面**
public class MainActivity extends Activity {
    private TextView textView;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_main);
        intent=getIntent();
        textView= (TextView) findViewById(R.id.textView);
//****getStringExtra 获取信息****
        textView.setText(intent.getStringExtra(Config.INTENT_TO));
        Button btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(MainActivity.this,Second_Activity.class);
                startActivityForResult(intent,12);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            String s=data.getStringExtra("sss");
            textView.setText(s);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("myApplication", "onStart ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("myApplication", "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("myApplication", "onResume ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("myApplication", "onPause ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("myApplication", "onStop ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("myApplication", "onDestroy ");
    }
}

//静态常量
public class Config {
    public static final String INTENT_TO="qwe123asd";
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值