20170120 Toast功能練習 (猜謎遊戲)
這個Toast顯示方式,有兩種出現時間可以選
LENGTH_SHORT顯示2秒和LENGTH_LONG顯示3.5秒(程式碼為黃色)
但如果要讓顯示可以立即顯示,那麼程式碼如紅色字體那段
//用Toast顯示答案 猜謎No38
//20170120 劉彥廷
package com.example.user.myapplication38;
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener{
String[] queArr = {"什麼門永遠關不上?","什麼東西沒人愛吃?","什麼瓜不能吃?","什麼布切不斷?"
,"什麼屬最愛乾淨?","偷什麼不犯法?"};
String[] ansArr = {"球門","吃虧","傻瓜","瀑布","環保署","偷笑"};
ListView lv;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, queArr);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
//這裡建立Toast物件和顯示出來
tos = Toast.makeText(this,"",Toast.LENGTH_SHORT);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(this,"答案:" +ansArr[pos],
Toast.LENGTH_SHORT).show();
tos.setText("答案:"+ansArr[pos]);
tos.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.myapplication38.MainActivity"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv" />
</LinearLayout>
strings.xml
<resources>
<string name="app_name">猜謎遊戲(No38)</string>
</resources>
LENGTH_SHORT(顯示2秒)
LENGTH_SHORT(立即顯示)
LENGTH_SHORT顯示2秒和LENGTH_LONG顯示3.5秒(程式碼為黃色)
但如果要讓顯示可以立即顯示,那麼程式碼如紅色字體那段
//用Toast顯示答案 猜謎No38
//20170120 劉彥廷
package com.example.user.myapplication38;
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener{
String[] queArr = {"什麼門永遠關不上?","什麼東西沒人愛吃?","什麼瓜不能吃?","什麼布切不斷?"
,"什麼屬最愛乾淨?","偷什麼不犯法?"};
String[] ansArr = {"球門","吃虧","傻瓜","瀑布","環保署","偷笑"};
ListView lv;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, queArr);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
//這裡建立Toast物件和顯示出來
tos = Toast.makeText(this,"",Toast.LENGTH_SHORT);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(this,"答案:" +ansArr[pos],
Toast.LENGTH_SHORT).show();
tos.setText("答案:"+ansArr[pos]);
tos.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.myapplication38.MainActivity"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv" />
</LinearLayout>
strings.xml
<resources>
<string name="app_name">猜謎遊戲(No38)</string>
</resources>
LENGTH_SHORT(顯示2秒)
留言
張貼留言