您的位置:首页 > 移动开发 > Android开发

寒假练习一

2016-01-24 21:48 375 查看
package com.example.first;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.InputFilter.LengthFilter;
import android.R.integer;
import android.R.string;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

public static int flag = 0;
private TimePicker tPicker = null;
private TextView tV = null;
private Spinner spinner = null;
private Spinner spinner1 = null;
private ProgressBar pBar = null;
private ProgressBar pbl = null;
private Button button = null;
private String[][] array = new String[][] { { "娇弱", "文艺", "聪慧" },
{ "贤惠", "尊贵", "优雅" }, { "可卿", "叹息", "命苦" } };

private Button btButton = null;
private int Max = 100;
private int current = 1;

Handler handler = new Handler() {
@Override
public void handleMessage(Message arg0) {
// TODO Auto-generated method stub
if (arg0.what == 0) {
pBar.setVisibility(View.VISIBLE);
// Toast.makeText(MainActivity.this,
// "正在加载数据...",Toast.LENGTH_SHORT);
} else if (arg0.what == 1) {
pBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "数据加载完毕...",
Toast.LENGTH_SHORT).show();
} else if (arg0.what == 2) {
pbl.setProgress(current);
}
else if (arg0.what == 3) {
Toast.makeText(MainActivity.this, "数据加载完毕...",
Toast.LENGTH_SHORT).show();
}

}
};

private ArrayAdapter<CharSequence> adapter = null;

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

tV = (TextView) findViewById(R.id.tv);
spinner = (Spinner) findViewById(R.id.sp);
spinner1 = (Spinner) findViewById(R.id.sp1);
button = (Button) findViewById(R.id.bt2);
pBar = (ProgressBar) findViewById(R.id.pb);
pbl = (ProgressBar) findViewById(R.id.pbl);
btButton = (Button) findViewById(R.id.bt_ceshitiao);

btButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
flag = 1;
new Thread() {

@Override
public void run() {
// TODO Auto-generated method stub
// flag = 1;
while (current<=100) {
handler.sendEmptyMessage(2);
sleeping_1();
}
handler.sendEmptyMessage(3);

}
}.start();
}
});

if (flag == 1) {
System.out.println("lllllllllllllllllllllllllllll");

}

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "数据正在加载...",
Toast.LENGTH_SHORT).show();
handler.sendEmptyMessage(0);

new Thread() {

@Override
public void run() {
// TODO Auto-generated method stub
// super.run();
sleeping();
System.out.println("nihao");

handler.sendEmptyMessage(1);

}
}.start();

}
});

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
/* tV.setText(arg0.getItemAtPosition(arg2).toString()); */

adapter = new ArrayAdapter<CharSequence>(MainActivity.this,
android.R.layout.simple_spinner_item, array[arg2]);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
tV.setText("什么都没有选中");

}
});
/* tPicker = (TimePicker) findViewById(R.id.tp); */
/*
* tPicker.setOnTimeChangedListener(new OnTimeChangedListener() {
*
* @Override public void onTimeChanged(TimePicker arg0, int arg1, int
* arg2) { // TODO Auto-generated method stub tV.setText(arg1 + " " +
* arg2); } });
*/
}

public void nide(View view) {

// Toast.makeText(MainActivity.this, "hello",Toast.LENGTH_SHORT).show();
Toast toast = Toast.makeText(MainActivity.this, "nihao",
Toast.LENGTH_SHORT);
LinearLayout ll = (LinearLayout) toast.getView();
ImageView iView = new ImageView(MainActivity.this);
iView.setImageResource(R.drawable.ic_launcher);
ll.addView(iView, 0);
toast.show();
}

public void sleeping() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void sleeping_1() {
try {
Thread.sleep(30);

current++;

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void ceshitiao(View view) {
System.out.println("dddddddddddddd");
flag = 1;
}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Spinner
android:id="@+id/sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@+array/strArray" />

<Spinner
android:id="@+id/sp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

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

<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />

<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+string/loading" />

<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="nide"
android:text="@+string/ceshi" />

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
android:id="@+id/bt_ceshitiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+string/ceshitiao"

/>
<ProgressBar
android:id="@+id/pbl"

android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal" />
</LinearLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">第一个</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

<string-array name = "strArray">
<item >林黛玉</item>
<item >薛宝钗</item>
<item >秦可卿</item>
</string-array>
<string name = "tips">
我是大帅哥
</string>
<string name ="loading">
加载数据
</string>
<string
name = "ceshi">
测试Toast
</string>
<string name = "ceshitiao">
测试loading条
</string>

</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android loading