One of the secrets of Android Development: creating clickable buttons

I feel it necessary to learn about mobile phone development, whether for future work needs or current company projects.

Of course, the beginning of anything new must be accompanied by the first HelloWorld, and Android learning is no exception. Since it has just begun, I won't describe it too much.

For the IDE: ADT developed by Android, it's a little confused at the first glance. However, after reading the introduction of various directory structures on the Internet, we slowly understand that for this example, we need to pay special attention to two places, one is the SRC directory and the other is the layout directory under the res directory. The SRC directory places the code behind source code, while the layout directory places the XML foreground configuration file.

Since the function we want to achieve is to click the button, and then "Hello world!" will be displayed in EditText. Let's first open the layout file, drag and drop a button, and then drag and drop an EditText. The final XML file structure is as follows:

<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="EditText" />

</RelativeLayout>

Then, in the background code file, we need to introduce two namespaces:

import android.widget.Button;
import android.widget.EditText;

All codes are as follows:

package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private Button myButton;
    private EditText myText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        myButton = (Button)findViewById(R.id.button1);
        myText = (EditText)findViewById(R.id.editText1);
        
        myButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                myText.setText("Hello World!");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Then click the run button and click the button in the virtual machine interface. The results are as follows:

That's all for this section. Let's continue to explore.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>