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";
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章