作业参考

一、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清空
            }
        });



    }

}

现在到了这里,其实我们就可以有很多自己玩的地方了,作业也不一定是死板的跟我的一样来做,可以自己改改,弄些有趣的地方,这样才能把安卓学的更好,下面分享一下我某位小朋友的作业(っ•̀ω•́)っ✎⁾⁾
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值