在android中加载图像和文本

时间:2018-05-04 17:36:36

标签: android image button text

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jatin.foreignlanguagefinal.French.FrenchFacts">

<ImageView
    android:id="@+id/imageFact"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:src="@drawable/booksf" />

<TextView
    android:id="@+id/textFact"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:layout_below="@+id/imageFact"
    android:gravity="center"
    android:maxLines="200"
    android:scrollbars="vertical"
    android:text="France is the world's most popular tourist destination – some 83.7 million visitors arrived in France, according to the World Tourism Organization report published in 2014, making it the world's most-visited country."
    android:textColor="@color/colorPrimary"
    android:textSize="20dp" />

<Button
    android:id="@+id/nextFact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textFact"
    android:layout_centerInParent="true"
    android:layout_margin="3dp"
    android:text="Next" />
</RelativeLayout>

public class FrenchFacts extends AppCompatActivity {

ArrayList<String> facts;
ImageView imageFacts;
TextView textFacts;
Button nextFact;
Map<String,Integer> mapFacts;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_french_facts);

    imageFacts = (ImageView) findViewById(R.id.imageFact);
    textFacts = (TextView) findViewById(R.id.textFact);
    nextFact = (Button) findViewById(R.id.nextFact)

    mapFacts = new HashMap<>();
    mapFacts.put("France is the world's most popular tourist destination – some 83.7 million visitors arrived in France, according to the World Tourism Organization report published in 2014, making it the world's most-visited country.",R.drawable.frances1);
    mapFacts.put("France is the largest country in the EU, and known as 'the hexagon' – with an area of 551,000 sq km it's almost a fifth of the EU’s total area, and due to its six-sided shape France is sometimes referred to as l’hexagone. About a quarter is covered by forest; only Sweden and Finland have more.",R.drawable.frances2 );
    mapFacts.put("Louis XIX was the king of France for just 20 minutes, the shortest ever reign – he ascended to the French throne in July 1830 after his father Charles X abdicated, and abdicated himself 20 minutes later in favour of his nephew, the Duke of Bordeaux.",R.drawable.frances3);
    mapFacts.put("Liberté, égalitié, fraternité meaning ‘liberty, equality and fraternity’ (or brotherhood) is the national motto of France – it first appeared around the time of the Revolution (1789–1799), and was written into the constitutions of 1946 and 1958. Today you’ll see it on coins, postage stamps and government logos often alongside ‘Marianne’ who symbolises the ‘triumph of the Republic’.",R.drawable.frances4);
    mapFacts.put("The French Army was the first to use camouflage in 1915 (World War I) – the word camouflage came from the French verb ‘to make up for the stage’. Guns and vehicles were painted by artists called camofleurs.",R.drawable.frances5);
    mapFacts.put("In France you can marry a dead person – under French law, in exceptional cases you can marry posthumously, as long as you can also prove that the deceased had the intention of marrying while alive and you receive permission from the French president.",R.drawable.frances6);
    mapFacts.put("France was the first country in the world to ban supermarkets from throwing away or destroying unsold food – since February 2016, shops must donate wastage to food banks or charities.",R.drawable.frances7);
    mapFacts.put("The Académie Française has aimed to preserve the French language since 1634 – by attempting to ban, somewhat unsuccessfully, foreign words such as blog, hashtag, parking, email and weekend. It was started by a small group of French intellects and officially recognised by Louis XIII in 1635.",R.drawable.frances8);
    mapFacts.put("At least 40 percent of the music on private radio stations must be of French origin – since 1996, the country’s top media regulator the Conseil Supérieur de L’Audiovisuel (CSA) has been charged with enforcing this French law. The CSA also requires half of the French music quota to be less than six months old.",R.drawable.frances9);
    mapFacts.put("France legalised same-sex marriage in 2013 – when President Françoise Holland signed the bill into law on 18 May 2013, France became the ninth country in Europe and 14th in the world to legalise same-sex marriage.",R.drawable.frances10);

nextFact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Random random = new Random();

            int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
            //String holder = facts.get(random.nextInt(facts.size()));
            textFacts.setMovementMethod(new ScrollingMovementMethod());





            for (Map.Entry<String,Integer> entry : mapFacts.entrySet()) {
                String text = entry.getKey();
                textFacts.setText(text);


                textFacts.setTextColor(color);

                int image = entry.getValue();

                Glide.with(getApplicationContext())
                        .load(image)
                            .into(imageFacts);


            }

        }


    });

我有一个imageview,textview和按钮。 单击按钮时,应显示不同的图像和文本。 我尝试过使用hashmap,但在迭代时,按钮单击只能工作一次。之后无论什么图像和文字都不会改变。那么,请告诉我其他方式可以显示文字和图像。

PS:我在网上搜索了很多,但找不到答案。请详细评论解决方案和代码。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

删除for循环,因为每次单击按钮时它只反映地图的最后内容,尽管它会遍历所有项目,但它发生的速度太快,以至于您无法找到更改。创建一个随机数,从地图index中选择,就像选择color文字一样。

    nextFact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Random random = new Random();
            int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
            //String holder = facts.get(random.nextInt(facts.size()));

            textFacts.setMovementMethod(new ScrollingMovementMethod());

            int index = random.nextInt(mapFacts.size());
            int image = getEntryByIndex(mapFacts,index).getValue();
            String text = getEntryByIndex(mapFacts,index).getKey();
            textFacts.setText(text);
            textFacts.setTextColor(color);
            Glide.with(getApplicationContext()).load(image).into(imageFacts);
        }
    });

你需要创建一个函数,它将为你提供索引的映射条目集,并在需要特定索引的条目集时调用它。

public static <String, Integer> Map.Entry<String, Integer> getEntryByIndex(Map<String, Integer> map, int i) {
    Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
    for(; i > 0; --i) {
        it.next();
    }
    return it.next();
}

答案 1 :(得分:0)

<强>被修改

 nextFact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Random random = new Random();
            int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));

        textFacts.setMovementMethod(new ScrollingMovementMethod());

        int i = random.nextInt(mapFacts.size());
        Object[] keys = map.keySet().toArray();
        String text = (String) keys[i];
        int id = mapFacts.get(keys[i]);
        textFacts.setText(text);
        textFacts.setTextColor(color);
        Glide.with(getApplicationContext()).load(id).into(imageFacts);
    }
});