新版ADT中增加了Fragment,倾向于在Fragment.xml中实现布局,而不是Activity.xml中
之前的视频教程中代码实现部分都是放在Activity的OnCreate方法中,在新的ADT工具中会报错。新版实现如下
Fragment.xml
<span style="font-size:14px;"><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"
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.android_4_3.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/TEL_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="31dp"
android:text="OK" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="14dp"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
</RelativeLayout></span>
对应的Java代码如下:
MainActivity.java
<span style="font-size:14px;">package com.example.android_4_3;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.telephony.PhoneNumberUtils;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
@SuppressLint("ShowToast") public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
protected static final int LENGTH_LONG = 0;
EditText myEditText=null;
Button myButton=null;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//rootView为fragment_main中所有控件集合
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
//获取TextView控件
myEditText = (EditText)rootView.findViewById(R.id.editText1);
//获取Button控件
myButton = (Button) rootView.findViewById(R.id.TEL_Button);
myButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
String TelNum=myEditText.getText().toString();
if(PhoneNumberUtils.isGlobalPhoneNumber(TelNum)){
Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse(TelNum));
}
else{
Toast.makeText(getActivity(), "error num", 1000)
.show();
}
}
}
);
return rootView;
}
}
}