Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed: Let's develop Android this time as well. I received a request this time, so I will answer the request and write about the basics of Android development in an easy-to-understand manner.
--A person who can write Java somehow. --People who want to develop Android. --Beginner in Android development.
--About the installation of Android Studio.
When you enter a name and press the button, the one entered in the display area below and the character string "-san, nice" are displayed.
I will make a screen with an xml file
activity_button_click_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_name" />
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<Button
android:id="@+id/btClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bt_click"/>
<TextView
android:id="@+id/tvOutput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text=""
android:textSize="25dp"/>
</LinearLayout>
--LinearLayout: Layout used when arranging parts vertically or horizontally. --TextView: Create a display area --EditText: Create an input area --Button: Make a button
①android:layout_width="〜〜〜"
②android:layout_height="〜〜〜"
① Width attribute ② Height attribute
A value that goes into ~ ~ ~
--match_parent: Expand to fill the screen. --wrap_content: It will be displayed in an appropriate size.
android:orientation="〜〜〜"
A value that goes into ~ ~ ~
--vertical: Arrange vertically --horizontal: side by side
android:text="value value"
* Usually "string".Specify the content to be output in "xml" and read the image from there
Example
android:text="@string/bt_click"
android:id="@+id/ID (name) for acquiring parts in the activity"
→ R value: The file in the res folder and the "" of that file@ + id/Since the value of "" is a management target, it is an int type integer that identifies the file or value.
It is automatically generated.
Example
android:id="@+id/tvOutput"
android:inputType="〜〜〜"
~ ~ ~: Type of inputType
**
I will write it in java.
ButtonClickSampleActivity.java
public class ButtonClickSampleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_click_sample);
Button button = findViewById(R.id.btClick);
ButtonClickListener listener = new ButtonClickListener();
button.setOnClickListener(listener);
}
/**
*A member class that describes what happens when a button is pressed
*/
private class ButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
EditText input = findViewById(R.id.etName);
String inputStr = input.getText().toString();
TextView output = findViewById(R.id.tvOutput);
output.setText(inputStr + "San, nice! !!" );
}
}
}
Inherit AppCompatActivity.
Use ** findViewById () ** and specify the R value (ID attached to the part) of the part as an argument.
EditText input = findViewById(R.id.etName)
Use ** getText (). ToString () **.
input.getString().toString()
Use ** setText ("embed string") **.
TextView output = findViewById(R.id.tvOutput);//Parts acquisition
output.setText(inputStr + "San, nice! !!" );
--Event: The user performs something on the screen. --Event handler: Processing performed in response to an event. --Listener: Validate this event.
(1) Create a listener class corresponding to each event as a member class. (2) Write the process in the method defined in the interface. ③ Set the listener by "new" the listener class.
Button button = findViewById(R.id.btClick);
ButtonClickListener listener = new ButtonClickListener();
button.setOnClickListener(listener);
private class ButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
EditText input = findViewById(R.id.etName);
String inputStr = input.getText().toString();
TextView output = findViewById(R.id.tvOutput);
output.setText(inputStr + "San, nice! !!" );
}
}
You can also write using anonymous functions (reference books are often written in this way). What they are doing is the same.
findViewById(R.id.btClick).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.etName);
String inputStr = input.getText().toString();
TextView output = findViewById(R.id.tvOutput);
output.setText(inputStr + "San, nice! !!" );
}
});
that's all. It was the basis of Android development. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.
Recommended Posts