简体   繁体   English

Android-选择项目的Toast消息

[英]Android - Toast message for Items chosen

How can I display a Toast messages for the items I've chosen in my layout? 如何为布局中选择的项目显示Toast消息?

For example, I have 15 ImageView s that are selectable , for example I've selected ImageView s 1, 2 and 3. A toast will appear when I click a button , "You've chosen ImageView s 1, 2, 3." 例如,我有15个selectable ImageView ,例如,我选择了ImageView的1、2和3。当我单击button “您已经选择ImageView的1、2、3”时,会出现一个吐司。

By the way, I've used setTag to know when an ImageView is selected. 顺便说一句,我已经使用setTag知道何时选择了ImageView I've setup setTag("1") for the views that is selected and setTag("0") for the rest. 我为选定的视图设置了setTag("1") ,其余的设置了setTag("0")

Sample code that I've tried: 我尝试过的示例代码:

public void onClick(View v) {
    String message = "You've chosen";
        if (v.getTag().toString().equals("1")) {
            message = message + " " + ivCircles[i].getId();
        }
    }
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    finish();
}

为每个图像设置contentDescription,并在选择其中一个图像后获取内容描述

Toast.makeText(context,imageview.getContentDiscription(),Toast.LENGTH_SHORT).show();

您需要使用ActionMode类。

This line should be in onClick method of ImageView 这行应该在ImageView onClick方法中

message = message + " " + view.getId();

This line should be a global variable 这行应该是一个全局变量

public static String message = "You've chosen";

After toast displayed in onClick method of the button, the global varibale should be initiated again 在按钮的onClick方法中显示吐司之后,应再次启动全局变量

Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
message = "You've chosen";

Try this way,hope this will help you to solve your problem. 尝试这种方式,希望这将帮助您解决问题。

activity_main.xml activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/lnrItems"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>
</ScrollView>

MainActivity.java MainActivity.java

public class MainActivity extends Activity {

    private LinearLayout lnrItems;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lnrItems = (LinearLayout) findViewById(R.id.lnrItems);

        for (int i=1;i<=15;i++){
            ImageView imageView = new ImageView(this);
            imageView.setId(i);
            imageView.setTag(String.valueOf(i));
            imageView.setImageResource(R.drawable.ic_launcher);
            imageView.setAdjustViewBounds(true);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this,"You've chosen ImageViews "+v.getTag().toString(),Toast.LENGTH_SHORT).show();
                }
            });
            lnrItems.addView(imageView);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM