常常在開發時,最頭痛的就是設計元件的UI了,現在有很多網友做好的第三方library,只要在gradle文件中貼上lib的來源,就可以開始使用別人設計好的lib了。
介紹一個網站,這個網站整合了許多在GitHub上一些熱門的library:红鸟网络Android团队Blog
<?php
$db_link=@mysqli_connect("資料庫位置","資料庫帳號","資料庫密碼");
if(!$db_link){
die("資料庫連線失敗<br>");
}else{
echo"資料庫連線成功<br>";
}
mysqli_query($db_link,"SET NAMES 'utf-8'"); //設定字元集與編碼為utf-8
$seldb=@mysqli_select_db($db_link,"資料庫名稱");
if(!$seldb){
die("資料庫選擇失敗<br>");
}else{
echo"資料庫選擇成功<br>";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form method="POST" action="login_php.php">
請輸入帳號:<input type="text" name="username"/><br>
請輸入密碼:<input type="Password" name="password"/><br>
<input type="submit" value="登入"/>
</form>
</body>
</html>
<?php
header('Content-Type: text/html; charset=utf-8');
$username=$_POST['username'];
$password=$_POST['password'];
require("conn_mysql.php");
$sql_query_login="SELECT * FROM employee where username='$username' AND password='$password'";
$result1=mysqli_query($db_link,$sql_query_login) or die("查詢失敗");
if(mysqli_num_rows($result1)){
$sql_query="SELECT * FROM books";
$result=mysqli_query($db_link,$sql_query);
echo "<table border=1 width=400 cellpadding=5>";
echo "<tr>
<td>書籍編號</td>
<td>書籍名稱</td>
<td>負責員工編號</td>
<td>價錢</td>
</tr>";
while($row=mysqli_fetch_array($result)){
echo "<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>";
}
echo"</table>";
}else{
echo"登入失敗";
}
?>
<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: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"
android:orientation="vertical" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="書名" />
<EditText
android:id="@+id/edt"
android:layout_width="150dp"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="search"
/>
</LinearLayout>
public class MainActivity extends Activity {
private Button btn;
private EditText edt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findviewbyid();
}
private void findviewbyid(){
btn = (Button)findViewById(R.id.btn);
edt = (EditText)findViewById(R.id.edt);
btn.setOnClickListener(btnlistener);
}
private View.OnClickListener btnlistener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Loose Coupling
// Intent it = new Intent("com.mchen.Activity2");
// it.putExtra("bookName", edt.getText().toString());
Uri uri = Uri.parse("Tom:"+edt.getText().toString()); //使用自訂的Scheme
Intent it = new Intent(Intent.ACTION_VIEW,uri); //跳頁到DisplayBooks
startActivity(it);
}
};
@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;
}
}
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class BookInfo2 {
private int totalItems;
private List<items> items = new ArrayList<items>();
public BookInfo2(){
}
public BookInfo2(int totalItems,List<items> items){
this.totalItems = totalItems;
this.items = items;
}
public int getTotalItems() {
return totalItems;
}
public void setTotalItems(int totalItems) {
this.totalItems = totalItems;
}
public List<items> getItems() {
return items;
}
public void setItems(List<items> items) {
this.items = items;
}
}
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class items {
private volumeInfo volumeInfo;
public items(){
}
public items(volumeInfo volumeInfo){
this.volumeInfo = volumeInfo;
}
public volumeInfo getVolumeInfo() {
return volumeInfo;
}
public void setVolumeInfo(volumeInfo volumeInfo) {
this.volumeInfo = volumeInfo;
}
}
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class volumeInfo {
private String subtitle;
private String title;
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="5dp"
android:id="@+id/listview">
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtSubTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
public class DisplayBooks extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book);
listView = (ListView)findViewById(R.id.listview);
Log.i("mutin","action-"+this.getIntent().getAction());
Log.i("mutin", "data-"+this.getIntent().getData().getSchemeSpecificPart());
final String param = this.getIntent().getData().getSchemeSpecificPart();
int cpuCount = Runtime.getRuntime().availableProcessors(); //取得可以建立Thread的最大數量
ExecutorService es = Executors.newFixedThreadPool(cpuCount); //建立Thread
Future<String> f = es.submit(new Callable<String>(){ //可以取得Callable回傳結果
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
URL url = new URL("https://www.googleapis.com/books/v1/volumes?q="+param);
java.net.URLConnection con = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String data = null;
StringBuffer result = new StringBuffer();
while((data=br.readLine())!=null){
result.append(data);
}
return result.toString();
}
});
try{
String result = f.get();
es.shutdown();
//產生Jackson函式庫負責解析的ObjectMapper類別
ObjectMapper mapper = new ObjectMapper();
//呼叫ObjectMapper的readValue方法進行Json格式轉換為Java資料的工作
BookInfo2 bi = mapper.readValue(result,BookInfo2.class);
HashMap<String,String> map;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
for(items item:bi.getItems()){
Log.i("mutin", "Title="+item.getVolumeInfo().getTitle());
Log.i("mutin", "SubTitle="+item.getVolumeInfo().getSubtitle());
map = new HashMap<String,String>();
map.put("title", "Title:"+item.getVolumeInfo().getTitle());
map.put("subTitle", "SubTitle:" + item.getVolumeInfo().getSubtitle());
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.row,new String[]{"title","subTitle"},new int[]{R.id.txtTitle,R.id.txtSubTitle});
listView.setAdapter(adapter);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
PS:用HightLight上色程式碼,格式會跑掉,請見諒
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
將動畫添加到view上:
view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="0%"
android:pivotY="100%"
android:toXScale="1.5"
android:toYScale="1.5" />
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="-100%"
android:fromYDelta="0"
android:toXDelta="100%p"
android:toYDelta="0" />
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" />
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="2000"/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000" />
</set>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" />
<RadioGroup
android:id="@+id/id_radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/id_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center_horizontal"
android:text="page1"
android:onClick="onClick"/>
<RadioButton
android:id="@+id/id_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center_horizontal"
android:text="page2"
android:onClick="onClick"/>
<RadioButton
android:id="@+id/id_three"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center_horizontal"
android:text="page3"
android:onClick="onClick"/>
<RadioButton
android:id="@+id/id_four"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center_horizontal"
android:text="page4"
android:onClick="onClick"/>
</RadioGroup>
</RelativeLayout>
public class MainActivity extends FragmentActivity {
private RadioButton id_one,id_two,id_three,id_four;
private int page = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findviewbyid();
changeFragment(DetailsFragment.newInstance(page));
}
public void onClick(View view){
switch (view.getId()){
case R.id.id_one:
changeFragment(DetailsFragment.newInstance(1));
break;
case R.id.id_two:
changeFragment(DetailsFragment.newInstance(2));
break;
case R.id.id_three:
changeFragment(DetailsFragment.newInstance(3));
break;
case R.id.id_four:
changeFragment(DetailsFragment.newInstance(4));
break;
}
}
private void findviewbyid(){
id_one = (RadioButton)findViewById(R.id.id_one);
id_two = (RadioButton)findViewById(R.id.id_two);
id_three = (RadioButton)findViewById(R.id.id_three);
id_four = (RadioButton)findViewById(R.id.id_four);
}
private void changeFragment(Fragment f) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, f);
transaction.commitAllowingStateLoss();
}
protected void onStart(){
super.onStart();
Log.i("tag","Activity onStart()");
}
protected void onResume(){
super.onResume();
Log.i("tag","Activity onResume()");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:text="fragment"
android:gravity="center"
android:textSize="20sp"/>
</LinearLayout>
public class DetailsFragment extends Fragment {
private View v;
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("tag","Fragment onCreateView()");
v = inflater.inflate(R.layout.fragment_tmp, container, false);
TextView text = (TextView)v.findViewById(R.id.text_view);
text.setText("Page" + getShownIndex());
return v;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" />
<RadioGroup
android:id="@+id/id_radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/id_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center_horizontal"
android:text="page1"
android:onClick="onClick"/>
<RadioButton
android:id="@+id/id_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center_horizontal"
android:text="page2"
android:onClick="onClick"/>
</RadioGroup>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private RadioButton id_one,id_two;
private Fragment1 mFragment01;
private Fragment2 mFragment02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findviewbyid();
mFragment01 = new Fragment1();
mFragment02 = new Fragment2();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, mFragment01,"TAG-mFragment01");
transaction.commitAllowingStateLoss();
}
private void findviewbyid(){
id_one = (RadioButton)findViewById(R.id.id_one);
id_two = (RadioButton)findViewById(R.id.id_two);
}
public void onClick(View view){
switch (view.getId()){
case R.id.id_one:
chaangefragment(mFragment01,"TAG-mFragment01");
break;
case R.id.id_two:
chaangefragment(mFragment02,"TAG-mFragment02");
break;
}
}
public void chaangefragment(Fragment fragment,String tag){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
List fragments = fragmentManager.getFragments();
Fragment tmp=null;
for (Fragment f: fragments){
if (f!=null && f.isVisible()) {
System.out.println("hide"+f.getTag());
tmp=f;
}
}
// Fragment fragment = getSupportFragmentManager().findFragmentByTag("TAG-mFragment01");
// System.out.println(fragment);
if (fragment.isAdded()){
transaction.hide(tmp).show(fragment).commit();
}else {
transaction.hide(tmp).add(R.id.fragment_container,fragment,tag).commit();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt"
android:text="fragment"/>
</LinearLayout>
public class Fragment1 extends Fragment {
private TextView txt;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.i("tag","Fragment1 onCreateView");
return inflater.inflate(R.layout.fragment_layout, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("tag","Fragment1 onActivityCreated");
txt = (TextView)getView().findViewById(R.id.txt);
txt.setText("fragment1");
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i("tag","Fragment1 onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("tag","Fragment1 onCreate");
}
@Override
public void onStart() {
super.onStart();
Log.i("tag","Fragment1 onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i("tag","Fragment1 onResume");
}
}
public class Fragment2 extends Fragment{
private TextView txt;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.i("tag","Fragment2 onCreateView");
return inflater.inflate(R.layout.fragment_layout, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("tag","Fragment2 onActivityCreated");
txt = (TextView)getView().findViewById(R.id.txt);
txt.setText("fragment2");
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i("tag","Fragment2 onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("tag","Fragment2 onCreate");
}
@Override
public void onStart() {
super.onStart();
Log.i("tag","Fragment2 onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i("tag","Fragment2 onResume");
}
}
執行順續page1(起始) -> page2 -> page1 -> page2
執行結果:
可以看到fragment1和fragment2個別被建立1次後,之後再呼叫時就不會再被建立新的Fragment了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.user.service.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/txt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private TextView txt;
private static final int msgKey1 = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.txt);
Thread t = new Thread(runnable);
t.start();
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
do{
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = msgKey1;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}while (true);
}
};
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case msgKey1:
long time = System.currentTimeMillis();
Date date = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 EEE");
txt.setText(format.format(date));
break;
default:
break;
}
}
};
}
public class MainActivity extends AppCompatActivity {
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.txt);
Thread t = new Thread(runnable);
t.start();
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
try {
while (true) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// update TextView here!
long time = System.currentTimeMillis();
Date date = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 EEE");
txt.setText(format.format(date));
}
});
}
} catch (InterruptedException e) {
}
}
};