Android programming Button control with Toast control usage analysis
- 2020-12-07 04:24:13
- OfStack
An example of Android programming Button control with Toast control is presented in this paper. To share for your reference, the details are as follows:
In this chapter tutorial, we will learn how to use the Button control and, incidentally, the Toast prompt control.
In Android application development, the user interaction control we use most is probably Button, and the event we use most is estimated to be onclick events.
These events are also the simplest ones. We can usually call them via google's built-in API interface. Let's see how to do this.
Step 1. Create a new project, Ep. Toast. I have used the default name of the activity and the main view.
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:layout_marginRight="61dp"
android:text=" determine " />
</RelativeLayout>
Inside is a text label and a button control.
Step 2. We write the core file - ES29en.java, and I'll comment it as much as I can here:
package com.example.ep.toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView txtview1;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind a text label control
txtview1=(TextView)findViewById(R.id.textView1);
// Bind button control
btn1=(Button)findViewById(R.id.button1);
// add 1 Six click event listeners and instantiated 1 A listener for click events
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Click the button to set the text label
txtview1.setText("Joven");
// Click the button and a prompt box will pop up. The parameters are (binding activity, prompt content, display time).
Toast.makeText(MainActivity.this, "Joven", Toast.LENGTH_LONG).show();
}
});
}
}
Finally, we can see what happens.
Okay, so that's the end of this chapter, although these are all very basic things. But no matter how big and complex a project is, it's built on these things. Knowledge is the accumulation of the essence of change.
I hope this article has been helpful in Android programming.