我们在使用Android应用的时候,同样Toast最多的应用就是获取网络的数据,因为网络的数据获取不是立刻的,而是需要一定时间的,而且用户希望当程序加载完网络数据后自动显示。例如,图片的下载成功和网络数据请求等。
步骤如下:
(1)修改activity_main.xml文件,代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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=".MainActivity" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<EditText
android:id="@+id/etImageUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入图片的地址"
android:text="@string/address"/>
<Button
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/button_bg"
android:onClick="viewImage"
android:text="浏览" />
</LinearLayout>
(2) 修改MainActivity.java文件,代码如下:
public class MainActivity extends Activity {
private EditText etImageUrl;
private ImageView ivImage;
public static final int SHOWIMAGE=1;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SHOWIMAGE:
Bitmap bitmap=(Bitmap) msg.obj;
ivImage.setImageBitmap(bitmap);
3
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
etImageUrl=(EditText) findViewById(R.id.etImageUrl);
ivImage=(ImageView) findViewById(R.id.ivImage);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//1:初始程序:
/*public void viewImage(View view)
{
String path=etImageUrl.getText().toString();//把图片路径转换成字符串
if(TextUtils.isEmpty(path))
{
/*
* question:对于一个UI界面中,当判断用户是否输入用户名或密码时,我们常用TextUtils.isEmpty()方法来判断;但有时也可以用这个equals()方法,都可以来判断EditText中是否为空,但有时很纠结,不知道这两种方法中哪个比较好?为什么?
answer:仔细读官方的API: Returns true if the string is null or 0-length. 因为你从EditText返回的是一个变量。如果这个变量本身为null值,那么你掉它的equals方法是要报错的。但是如果你调用TextUtils.isEmpty() 把这个变量作为参数传进去。
只要这个参数为空或者为"",都会返回真。所以,用官方给的更加严谨。
而且,也十分方便。因为你单独去判断你还不是要写一个if语句判断。返回的还是一个boolean值
Toast.makeText(this, R.string.NOnull, Toast.LENGTH_LONG).show();//如果输入路径为空,就弹出Toast
}else{
//不为空,连接服务器,请求获得图片
try{
URL url=new URL(path);
//发出http请求
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");//设置提交方式
//设置连接超时时间
httpURLConnection.setConnectTimeout(5000);//这时,我们设置为超时时间为5秒,如果5秒内不能连接就被认为是有错误发生.
int responsecode=httpURLConnection.getResponseCode();
if(responsecode==200){
InputStream inputstream=httpURLConnection.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputstream);
ivImage.setImageBitmap(bitmap);
}else{
Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show();
}
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException E){
E.printStackTrace();
}
}
*/
public void viewImage(View view){
final String imageUrl=etImageUrl.getText().toString();
if(TextUtils.isEmpty(imageUrl)){
Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_LONG).show();
}else{
new Thread(){
public void run() {
try {
URL url=new URL(imageUrl);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(5000);
int responseCode=httpURLConnection.getResponseCode();
if(responseCode==200){
InputStream inputStream=httpURLConnection.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
Message message=new Message();
message.what=SHOWIMAGE;
message.obj=bitmap;
//ivImage.setImageBitmap(bitmap);
handler.sendMessage(message);
}else{
Toast.makeText(MainActivity.this, "显示图片失败", Toast.LENGTH_LONG).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
效果图如下: