很早就想学习android编程了,奈何太懒。
现在我跟android官方文档做了个项目,挺简单的。
我只贴上核心代码吧。
它有两个活动,在第一个活动中输入文本,第二个活动中展示。
第一个活动(MainActivity)
类文件
package com.example.myhelloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myhelloworld.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send Button*/
public void sendMessage(View view) {
/** 新建一个活动intent*/
Intent intent = new Intent(this, DisplayMessageActivity.class);
/** 获取可编辑文本框(整体元素),利用id获取。并将值赋给editText*/
EditText editText = (EditText) findViewById(R.id.mytext);
/** 声明变量message并赋值editText里的文本内容*/
String message = editText.getText().toString(); // .getText().toString() 获取文本
/** 将常量EXTRA_MESSAGE重新赋值为message,并且插入到intent对象中*/ // ps.常量可以重新赋值?
intent.putExtra(EXTRA_MESSAGE, message);
/** 启动活动intent,会自动跳转到DisplayMessageActivity*/
startActivity(intent);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="9dp"
android:onClick="sendMessage"
android:text="Button"
app:layout_constraintBaseline_toBaselineOf="@+id/mytext"
app:layout_constraintStart_toEndOf="@+id/mytext" />
</androidx.constraintlayout.widget.ConstraintLayout>
第二个活动(DisplayMessageActivity)
类文件
package com.example.myhelloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
/** 活动创建时运行此函数*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// 获取传值
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// 展示
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="176dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
以上。