草庐IT

Android: 有没有什么好的方法可以创建类似 Toast 的对话框?

coder 2023-12-03 原文

我想向用户显示一些信息,但我不希望在用户点击其他地方或按下后退按钮之前关闭这些信息。我意识到最明显的选择是在对话框中显示文本(而不是 toast )。我希望我的对话框类似于系统 Toast。我认识到我可以只复制 transient_notification.xml 布局(及其相关资源),但由于 Toast 样式因设备和操作系统而异,因此不太可能产生良好的匹配。

那么,有没有一种好的方法来创建一个继承系统Toast样式的Dialog?

最佳答案

或者你可以使用自定义布局

显示 toast 的自定义方法

public static Toast currentToast;
/**
 * Use a custom display for Toasts.
 *
 * @param message
 */
public static void customToast(String message) {
    // Avoid creating a queue of toasts
    if (currentToast != null) {
        // Dismiss the current showing Toast
        currentToast.cancel();
    }       
    //Retrieve the layout Inflater
    LayoutInflater inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //Assign the custom layout to view
    View layout = inflater.inflate(R.layout.custom_toast, null);
    //Return the application context
    currentToast = new Toast(context.getApplicationContext());
    //Set toast gravity to center
    currentToast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
    //Set toast duration
    currentToast.setDuration(Toast.LENGTH_LONG);
    //Set the custom layout to Toast
    currentToast.setView(layout);
    //Get the TextView for the message of the Toast
    TextView text = (TextView) layout.findViewById(R.id.text);
    //Set the custom text for the message of the Toast
    text.setText(message);
    //Display toast
    currentToast.show();
    // Check if the layout is visible - just to be sure
    if (layout != null) {
        // Touch listener for the layout
        // This will listen for any touch event on the screen
        layout.setOnTouchListener(new OnTouchListener() {           
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // No need to check the event, just dismiss the toast if it is showing
                if (currentToast != null) {
                    currentToast.cancel();
                    // we return True if the listener has consumed the event
                    return true;
                }   
                return false;
            }
        });
    }
}

custom_toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip"
    android:background="@drawable/custom_toast_shape">

    <TextView
        android:id="@+id/title" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="@color/blue"
        android:textSize="16sp"
        android:text="@string/app_name" 
    />

    <View
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:background="@color/blue" 
    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:maxEms="15"
        android:gravity="center_horizontal"
        android:id="@+id/text" 
    />
</LinearLayout>

要使用它只需调用

Utils.customToast("Just a test to see if toast is working!\nPerfect");

编辑 我稍微修改了方法。现在它不会创建 toast 队列,并且会在触摸时被取消,就像普通的 toast 一样。

如果其他人可以改进它,请随意这样做:)

关于Android: 有没有什么好的方法可以创建类似 Toast 的对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16068081/

有关Android: 有没有什么好的方法可以创建类似 Toast 的对话框?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  5. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  6. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  7. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  8. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  9. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  10. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

随机推荐