Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize pop-up window setting by Android Studio

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to achieve pop-up window setting in Android Studio". In daily operation, I believe many people have doubts about how to achieve pop-up window setting in Android Studio. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve pop-up window setting in Android Studio". Next, please follow the editor to study!

1. The most commonly used pop-up window display

If you use Toast directly, a short delay pop-up window will appear at the bottom of the screen.

First, lay out the control in Activity and give it to id, then declare id in Mainactivity, bind id, then set click event, and finally add the code of pop-up window.

This is to add this pop-up window directly to the click event and OK it.

Toast.makeText (getApplicationContext (), "Toast", Toast.LENGTH_SHORT). Show (); / / the pop-up window at the bottom of the screen

two。 This is to display a short delay pop-up window in the middle of the screen.

This is similar to the one above. Just modify it a little bit.

Add the following code to the click event

Toast toastCenter = Toast.makeText (getApplicationContext (), "centered Toast", Toast.LENGTH_SHORT); / / screen centered pop-up window toastCenter.setGravity (Gravity.CENTER,0,0); toastCenter.show ()

3. The third kind of display effect is the same as the first one, which is a short delay pop-up window at the bottom of the screen, but one difference is that if it is the first kind of pop-up window, its pop-up window will be queued up to display three times if it is clicked three times. obviously this effect is not what we want, so how to make it click many times, but only show a short delay pop-up window, then we have to use today's third pop-up window code.

Here I created a new util file in Java, and then wrote a Toasttutil, which can be called directly later.

This is the code in Toasttutil.

Package util; import android.content.Context;import android.widget.Toast; public class Toastutil {public static Toast mtoast; public static void showMsg (Context context,String msg) {if (mtoast==null) {mtoast= Toast.makeText (context,msg,Toast.LENGTH_LONG);} else {mtoast.setText (msg);} mtoast.show ();}}

Finally, just call the Toastutil in the click event.

Toastutil.showMsg (getApplicationContext (), "packaged Toast"); / / packaged Toast pop-up window will be offset by multiple clicks, only once

Finally, all the code in my Mainactivity.java is attached!

Package com.example.daytwo; import androidx.appcompat.app.AppCompatActivity;import util.Toastutil; import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.Toast; import java.util.zip.Inflater; public class ToastActivity6 extends AppCompatActivity {private Button mbtn_toast_1; private Button mbtn_toast_2; private Button mbtn_toast_3; private Button mbtn_toast_4 @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_toast6); mbtn_toast_1 = findViewById (R.id.btn_toast_1); mbtn_toast_2 = findViewById (R.id.btn_toast_2); mbtn_toast_3 = findViewById (R.id.btn_toast_3) Mbtn_toast_4 = findViewById (R.id.btn_toast_4); OnClick onClick = new OnClick (); mbtn_toast_1.setOnClickListener (onClick); mbtn_toast_2.setOnClickListener (onClick); mbtn_toast_3.setOnClickListener (onClick); mbtn_toast_4.setOnClickListener (onClick) } class OnClick implements View.OnClickListener {@ Override public void onClick (View view) {switch (view.getId ()) {case R.id.btn_toast_1: Toast.makeText (getApplicationContext (), "Toast", Toast.LENGTH_SHORT). Show (); / / the pop-up window break at the bottom of the screen Case R.id.btn_toast_2: Toast toastCenter = Toast.makeText (getApplicationContext (), "centered Toast", Toast.LENGTH_SHORT); / / screen centered pop-up window toastCenter.setGravity (Gravity.CENTER,0,0); toastCenter.show (); break Case R.id.btn_toast_3: Toast toastCustom = new Toast (getApplicationContext ()); LayoutInflater layoutInflater = LayoutInflater.from (ToastActivity6.this); / / View.inflate (R.layout.) / / toastCustom.setView (); break Case R.id.btn_toast_4: Toastutil.showMsg (getApplicationContext (), "packaged Toast"); / / packaged Toast pop-up window, multiple clicks will counteract it, and only one break will appear. At this point, the study on "how to set up the pop-up window in Android Studio" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report