【发布时间】:2018-05-04 17:36:36
【问题描述】:
<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);
}
}
});
我有一个图像视图、文本视图和按钮。 单击按钮时,应显示不同的图像和文本。 我曾尝试使用 hashmap,但在迭代时,按钮单击只能工作一次。之后无论什么图像和文字都不会改变。所以,请告诉我还有什么其他方式可以显示文本和图像。
PS:我在网上搜索了很多,没有找到答案。请详细评论解决方案和代码。任何帮助表示赞赏。
【问题讨论】:
-
分享你的代码。
-
为什么人们对评论投反对票。我只是说我到处寻找解决方案,但找不到。为什么你们不能简单地给我解决方案。告诉我我做错了什么。给我拒绝投票的理由。
-
这不是我 :P 但请更详细地描述您的问题并提供您的 Java 代码,您目前为止。有多少视图?我在您的布局中只看到一个?还是您在谈论 ListView?你说的显示是什么意思?它们应该已经从您的布局中可见!究竟是什么,你想要做什么。更具体地描述您的问题。很不清楚!
-
可能是您的图像和文字随着地图对象的最后内容而变化。每次单击时,您都会遍历地图,当循环完成时,您会得到相同的结果。我认为最好选择随机选择选项来获取更改。
-
@Locdoc01 对不起,如果我不清楚。我的 Java 代码位于布局代码下方。我不知道如何将它们分开。所以,我正在尝试制作一个事实应用程序,它在布局上具有三个元素。一个图像、一个文本和一个按钮。单击按钮时,图像和文本都应更改。我采用了 HashMap
。字符串存储文本,整数存储图像。然后我尝试在单击按钮时迭代哈希图,并将键存储在字符串中,将值存储在 int 变量中。然后我在各自的布局中显示了字符串和整数。但是点击时我只能从地图中看到一个条目。