新版ADT中fragment布局与代码实现的关系
来源:互联网 发布:mac homebrew 是什么 编辑:程序博客网 时间:2024/05/17 04:22
新版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; } }}
0 0
- 新版ADT中fragment布局与代码实现的关系
- fragment的应用,新版ADT改版后界面处理
- Android新手:新版ADT处理Fragment、actionbar和style的方法
- Fragment与自定义布局实现类似tab的效果
- Fragment与自定义布局实现类似tab的效果
- Android--ADT与SDK之间的关系
- android中ADT、SDK、APK的关系
- android中ADT和SDK的关系
- android中ADT和SDK的关系
- android中ADT和SDK的关系
- android中ADT和SDK的关系
- Android中ADT和SDK的关系
- android中ADT和SDK的关系
- Android中ADT和SDK的关系
- 关于xml布局中Fragment与 FrameLayout的理解
- Fragment与FragmentActivity的关系
- Fragment与FragmentActivity的关系
- Fragment与FragmentActivity的关系
- 机器学习之抽样检查的重要性
- iOS开发网络篇—简单介绍ASI框架的使用
- 导入第三方类库的问题汇总
- POJ 1300 Door Man(欧拉通路)
- drupal常用模块
- 新版ADT中fragment布局与代码实现的关系
- Jquery ajax 得到返回值
- mono 下连接 mysql
- clojure实战—— :pos & :pre断言
- 2013Excel画甘特图
- velociy 插件安装
- NYOJ 86 找球号
- qemu 内核启动输出结果保存
- 黑马程序员———数组