Android studio review notes 1

Preface

        Two years ago, I accidentally joined a group of college students' innovation training program led by the head of the industrial engineering department. The task I was assigned was to write an Android App as the connector of the system. So I started learning Java and Android studio at that time. The result of the project was that the App I wrote applied for a software copyright with me as the first author. It can be said that the more than half a year of study almost changed the direction of my life, because it gave me some skills foundation on the issue of whether to transcode, so much so that I was furious at the time to finish 100 Liku algorithm questions. , and relied on his resume to apply for campus interns in client development positions such as Tencent, Alibaba, and ByteDance. After that, I gradually realized that I was really not suitable for the direction of pure transcoding. So the learning of Android development comes to an end.

        Why do I want to learn it again recently? The main reason is that I want to fulfill my old wish and use Android studio to develop an App as a remote control. After all, I have already entered the road of electronic control, and I have also done Android development for a short period of time, so I just want to pick up what I have learned before and put it to good use!

PS: This article is just a note to facilitate my own study. Since I did not record some relevant content in my previous study, the previous study was based on CSDN blogger @Guo Lin (15 messages) guolin's blog_CSDN Blog-icon-default.png?t=M85B Android troubleshooting, Android essence tutorial, second edition of "The First Line of Code" written by a blogger in the essay field https://blog.csdn.net/guolin_blog/?type=blog . I also deleted all my previous demos when I changed computers, and the version of Android studio I used before was too low (based on Android7.0), and the recently installed AS is the latest version (based on Andriod11 and up, It's definitely not the latest, but it's pretty new anyway). So everything starts over. Therefore, this article is just a review record of my own, so that I can quickly search it in the future. It may not be of much help to everyone who reads the article, so please understand!

text

       First put some remaining app shells on your phone!

 These are mainly cosmetic projects, written for people to read, but actually not endowed with relevant functions, so I would like to call them shells. The app that I applied for software has written a lot of functions into it!

Always keep an eye on these folders:

Needless to say, the usefulness of the Manifest registry. All activities, windows, and some controls and what they call must be described in it.

 

In my impression, build.gradle has solved problems caused by version mismatches and so on, and you can see a lot of information in it.

 Commonly used folders are two large folders: java and res. One writes the backend and the other writes the frontend. Of course, there are sometimes crossovers. Since it is a random review, I will briefly remember some points that I have forgotten or that will be helpful in the future!

Listener onClick() method

There are two methods of anonymous class and interface class to implement registration: (The example here is a window jump after clicking a button)

/*匿名类方法*/
/*public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this,activity.class);
                    startActivity(intent);
            }
        });
    }
}*/
/*通过接口来实现*/
public class MainActivity extends AppCompatActivity  implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.button_1:
                Intent intent = new Intent(MainActivity.this,activity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}

Create a new window

When writing a new activity, you must register it in the registry and clearly indicate which activity is the main activity (<intent-filter>)

<activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>
<activity
            android:name=".activity"
            android:exported="true">
</activity>

 The use of common controls - taking AlertDialog as an example

public class activity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.button2:
                AlertDialog.Builder dialog = new AlertDialog.Builder(activity.this);
                dialog.setTitle("这是个对话框");
                dialog.setMessage("你好,我是饺子");
                dialog.setCancelable(false);
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.setNegativeButton("关闭", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
                break;
            default:
                break;
        }
    }
}

Run on your phone:

        Oh, I have to mention here that the new version of Android studio is very comfortable with layout settings. Two years ago, I was tortured to death because of the code problem of this layout. At that time, there was no way to use visual programming. But in the version I downloaded now, you can directly drag the control position and other settings, and it will not be "disobedient" like the old version. It is very convenient to use the visualization window! It can be said that it saved me a lot of effort to review this part, hahaha!

 


 Uh uh, it seems that there is not enough time, and the article does not have too many nutritious things. However, I am basically familiar with some of the previous things. After a while, I started to use Android to realize Bluetooth or wifi communication, and finally to the remote control of steering gear and motor! A little bit of expectation! 

Guess you like

Origin blog.csdn.net/weixin_47723114/article/details/128163757