<ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ProgressBar" />
<RelativeLayout 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" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="163dp" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/progressBar1" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="TextView" /> </RelativeLayout>
package com.myapp; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { private static final int REFRESH_SCREEN = 1; ProgressBar Progress; TextView txtView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // progressBar1 Progress = (ProgressBar) findViewById(R.id.progressBar1); // textView1 txtView = (TextView) findViewById(R.id.textView1); txtView.setVisibility(View.INVISIBLE); // Default Hide startScan(); // Sleep } public void startScan() { new Thread() { public void run() { try{ Thread.sleep(5000); hRefresh.sendEmptyMessage(REFRESH_SCREEN); }catch(Exception e){ } } }.start(); } Handler hRefresh = new Handler(){ public void handleMessage(Message msg) { switch(msg.what){ case REFRESH_SCREEN: Progress.setVisibility(View.INVISIBLE); // Hide ProgressBar ShowText(); break; default: break; } } }; public void ShowText() { txtView.setText("Welcome to My App"); txtView.setVisibility(View.VISIBLE); // Show Text } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewSwitcher1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableLayout> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tableLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Welcome to My App" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pic_a" android:layout_marginTop="10dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="CopyRight 2012 www.ThaiCreate.Com" android:textAppearance="?android:attr/textAppearanceSmall" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reload again" /> </TableLayout> </ViewSwitcher>
package com.myapp; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.ViewSwitcher; public class MainActivity extends Activity { ViewSwitcher Vs; private static final int REFRESH_SCREEN = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // viewSwitcher1 Vs = (ViewSwitcher) findViewById(R.id.viewSwitcher1); startScan(); // Reload again Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Vs.showPrevious(); startScan(); } }); } public void startScan() { new Thread() { public void run() { try{ Thread.sleep(5000); hRefresh.sendEmptyMessage(REFRESH_SCREEN); }catch(Exception e){ } } }.start(); } @SuppressLint("HandlerLeak") Handler hRefresh = new Handler(){ public void handleMessage(Message msg) { switch(msg.what){ case REFRESH_SCREEN: Vs.showNext(); break; default: break; } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewAnimator1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="LoadData" /> </TableLayout> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tableLayout2" android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableLayout> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tableLayout3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Welcome to My App" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pic_a" android:layout_marginTop="10dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textAppearance="?android:attr/textAppearanceSmall" /> </TableLayout> </ViewAnimator>
package com.myapp; import java.util.Date; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.annotation.SuppressLint; import android.app.Activity; import android.text.format.DateFormat; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.ViewAnimator; public class MainActivity extends Activity { ViewAnimator va; private static final int REFRESH_SCREEN = 1; String currTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // viewAnimator1 va = (ViewAnimator) findViewById(R.id.viewAnimator1); ShowStep1(); // Default Slow Step1 } public void ShowStep1() { // Reload Data Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { va.showNext(); // Show Load Progress Step2 ShowStep2(); } }); } public void ShowStep2() { // Progress Bar and Check Loading Data CheckLoadingData(); } // Check Loading Data public void CheckLoadingData() { new Thread() { public void run() { try{ // Do something // Eg : Load Data From Server Date d = new Date(); currTime = DateFormat.format("EEEE, MMMM d, yyyy ", d.getTime()).toString(); Thread.sleep(5000); hRefresh.sendEmptyMessage(REFRESH_SCREEN); }catch(Exception e){ } } }.start(); } @SuppressLint("HandlerLeak") Handler hRefresh = new Handler(){ public void handleMessage(Message msg) { switch(msg.what){ case REFRESH_SCREEN: va.showNext(); // Show Home Step 3 ShowStep3(); break; default: break; } } }; public void ShowStep3() { // textView2 TextView txtView2 = (TextView) findViewById(R.id.textView2); txtView2.setText("Current Time : " + currTime); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท