【问题标题】:How to remove first time toast when application launch?应用程序启动时如何删除第一次吐司?
【发布时间】:2013-10-20 10:42:32
【问题描述】:

我正在实现 Spinner,我的问题是当我启动应用程序时它会显示 toast,它会显示第一个元素。那时我没有从 spinner 中选择项目。

我喜欢这个。它在应用程序启动时首次显示马来西亚。

在string.xml中

 <string name="country_prompt">choose country</string>

    <string-array name="country_arrays">
        <item>Malaysia</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>Italy</item>
        <item>Singapore</item>
        <item>New Zealand</item>
        <item>India</item>
    </string-array>


<Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:entries="@array/country_arrays"
        android:prompt="@string/country_prompt"

        />

关于java文件

setContentView(R.layout.firstactivity);
    sp= (Spinner) findViewById(R.id.spinner1);
    sp.setOnItemSelectedListener(this)

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        // TODO Auto-generated method stub

        Toast.makeText(parent.getContext(), 
                "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

;

【问题讨论】:

    标签: android


    【解决方案1】:

    据我了解,您不希望 ToastActivity 第一次运行时显示。 onItemSelected 在创建 Activity 时运行,即使用户实际上并未选择任何内容。

    AFAIK,没有办法避免这种情况。但是,您可以轻松地在 onCreate() 中设置 boolean 标志并检查 onItemSelected 中的标志以决定是否运行代码,然后切换 onItemSelected 中的标志。

    像这样的

      setContentView(R.layout.firstactivity);
        sp= (Spinner) findViewById(R.id.spinner1);
        sp.setOnItemSelectedListener(this);
        firstRun = true;   // declare this as a member variable (before onCreate())
    
    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
            // TODO Auto-generated method stub
            if (!firstRun)
            {
                Toast.makeText(parent.getContext(), 
                    "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                    Toast.LENGTH_SHORT).show();
            }
            else
            {  firstRun = false;  }
    
        }
    

    【讨论】:

    • 你能再帮我一件事吗..我需要写文本(任何东西)定期的时间间隔..因为我想检查设备是否挂起
    • 实际上我在android中使用phonegap(从服务器获取数据)requular时间间隔制作应用程序。但该应用程序在10分钟后挂起
    • 所以如果我们继续写入或获取数据,我需要检查每个应用程序是否在 30 分钟后挂起
    • 我想我需要使用 Timer 并设置 sheduleAt fixedrate。
    • 我不太确定我了解您想要做什么,但如果获取数据需要 10 分钟,那么您的问题就更大了
    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多