作業參考

一、Intent作業

1.使用隱式Intent從AActivity跳轉至BActivity,並傳遞數字"250",將其打印。
2.BActivity返回AActivity時,傳遞字符"Yes!",將其打印

public class AActivity extends AppCompatActivity {
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.main_btn);
        textView = findViewById(R.id.text_1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { 
                Intent intent = new Intent("Start");
                String data = "123";
                intent.putExtra("value",data);
                startActivityForResult(intent, 1);

            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                   String getData = data.getStringExtra("getdata");
                  Log.d("AActivity",getData);
                }
                break;
            default:
        }
    }
}

由於這裏是隱式intent,故而要在AndroidManifest.xml中找到你創建的BActivity,添加如下代碼

<activity android:name=".BActivity">
            <intent-filter>
                <action android:name="Start"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

對應AActivity的xml佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AActivity">
    <Button
        android:id="@+id/main_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳轉到BActivity"
        android:textAllCaps="false"/>
</LinearLayout>

然後是BActivity中:

public class BActivity extends AppCompatActivity {
Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        button1  =findViewById(R.id.b_btn);
        Intent intent = getIntent();
        String data = intent.getStringExtra("value");
       Log.d("BActivity",data);

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent1 = new Intent();
        intent1.putExtra("getdata","yes");
        setResult(RESULT_OK,intent1);
        finish();
    }
});
    }

    @Override
    public void onBackPressed() {
        Intent intent1 = new Intent();
        intent1.putExtra("getdata","yes");
        setResult(RESULT_OK,intent1);
        finish();
    }
}

對應BActivityxml的佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="BActivity">
    <Button
        android:id="@+id/b_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳回到AActivity"
        android:textAllCaps="false"/>
</LinearLayout>

二、佈局按鈕作業

在這裏插入圖片描述
1.首先觀察整體,是一個從上往下的線性佈局,故而寫一個LinearLayout,orientation屬性設置爲horizontal
2.笑臉在水平中間,故而在ImageView中設置一個 android:layout_gravity="center_horizontal"屬性
3.身體明顯是一個相對佈局,故而我們寫一個RelativeLayout,接着我們以身體中心作爲參考點,來講各個部位寫出來
4.接下來顯示的是一段文字“請問我帥嗎?”我們直接用Textview顯示出來就可。
5.下面是EditText與Button按照3:1平分屏幕,這裏我們而可以用比例的就是LinearLayout的權重,因此我們寫一個LinearLayout,並且在裏面寫兩個空間EditText和Button
6.最下面的明顯的就是一個textView,但這個textView是用來動態顯示我們的回答的,故而具體邏輯等下我們在MainActivity中代碼裏面實現,這裏直接寫個TextView,不需要設置text屬性
佈局代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  <ImageView
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:src="@drawable/smile"
      android:layout_gravity="center_horizontal"
      />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        >
        <Button
            android:layout_width="75dp"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"/>
        <Button
            android:layout_width="75dp"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"/>
        <Button
            android:id="@+id/center"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerInParent="true"/>
        <Button
            android:layout_width="50dp"
            android:layout_height="75dp"
            android:layout_toLeftOf="@+id/center"
            android:layout_below="@+id/center"/>
        <Button
            android:layout_width="50dp"
            android:layout_height="75dp"
            android:layout_toRightOf="@+id/center"
            android:layout_below="@+id/center"/>

    </RelativeLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="請問我帥嗎?"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            />
        <Button
            android:id="@+id/show_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="顯示"/>


    </LinearLayout>

    <TextView
        android:id="@+id/show_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30dp"
        android:gravity="center"/>

</LinearLayout>

接下來是MainActivity中的代碼邏輯,處理我們在EditText輸入答案後,點擊顯示按鈕,會把Edittext中的答案顯示在我們最下面的TextView裏面,並且把EditText清空:

public class MainActivity extends AppCompatActivity {


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

        Button button = findViewById(R.id.show_btn); //找到我們的顯示button的id
        final TextView textView = findViewById(R.id.show_text);  //找到我們顯示的TextView的id
        final EditText editText = findViewById(R.id.editText);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = editText.getText().toString();  //利用getText方法獲取內容,並將改內容用toString方法轉化成String類型,存在content中
                textView.setText(content);  //TextView用setText方法顯示內容
                editText.setText("");  //EditText清空
            }
        });



    }

}

現在到了這裏,其實我們就可以有很多自己玩的地方了,作業也不一定是死板的跟我的一樣來做,可以自己改改,弄些有趣的地方,這樣才能把安卓學的更好,下面分享一下我某位小朋友的作業(っ•̀ω•́)っ✎⁾⁾
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章