碎片Fragment兼顾平板
嵌入在活动中的UI片段
例子:一个活动中添加两个碎片
新建一个左侧碎片布局和右侧碎片布局
left_fragment.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/button" androID:layout_gravity="center_horizontal" androID:text="button"/></linearLayout>
right_fragment.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:background="#00ffbb" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:textSize="20sp" androID:text="This is a fragment"/></linearLayout>
新建leftFragment和rightFragment类
public class leftFragment extends Fragment { @Nullable @OverrIDe public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.left_fragment, container, false); return vIEw; }}
public class RightFragment extends Fragment { @Nullable @OverrIDe public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.right_fragment, container, false); return vIEw; }}
inflater.inflate()
在主活动的界面中添加两个碎片
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <fragment androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:name="org.nuaa.fragmenttest.leftFragment" androID:ID="@+ID/left_fragment"/> <fragment androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:name="org.nuaa.fragmenttest.RightFragment" androID:ID="@+ID/right_fragment"/> </linearLayout>
使用name指定要添加的碎片类名
新建一个another_right_fragment.xml,内容与right_fragment.xml类似
AnotherRightFragment类也是与RightFragment类似,也是重写onCreateVIEw
修改主活动的布局,将右边改成FragmentLayout
<FrameLayout androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:ID="@+ID/right_layout"/>
修改主活动
public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{ @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); button button = (button) findVIEwByID(R.ID.button); button.setonClickListener(this); replaceFragment(new RightFragment()); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.button: replaceFragment(new AnotherRightFragment()); break; default: break; } } private voID replaceFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.ID.right_layout, fragment); transaction.commit(); }}
新建repalceFragment方法,修改FragmentLayout中的碎片
初始化replaceFragment(new RightFragment()),点击按钮后repalceFragment(new AnotherRightFragent())替换
动态添加碎片的步骤:
创建待添加的碎片实例replaceFragment(new XXXXX())获取FragmentManager,在活动中可以直接通过调用getSupportFragmentManager()方法获取开启一个事务,通过调用beginTransaction()方法开启向容器内添加或替换碎片,使用replace(),传入容器的ID和代添加的碎片实例提交事务,commit()在碎片中模拟返回栈
在commit()之前添加一句transaction.addToBackStack(null)
,参数是String名字,用于描述返回栈的状态。
RightFragment rightFragment = (RightFragment) getSupportFragmentManager().findFragmentByID(R.ID.right_fragment);
碎片中调用活动MainActivity activity = (MainActivity) getActivity();
有活动实例后,调用活动中的方法就容易了。另外当碎片中使用Context对象时,也可以用getActivity(),因为活动本身就是一个Context对象
碎片生命周期运行,暂停,停止,销毁
运行碎片可见,关联的活动处于运行状态,碎片也处于运行暂停
活动进入暂停状态时(由于另外一个未占满屏幕的活动被添加到栈顶),相关联的碎片就会进入暂停停止
当活动进入暂停状态,关联的碎片就会进入停止状态,或,
通过调用FragmentManager.remove()、repalce()方法将碎片从活动中移除,如果事务在提交前调用addToBackStack(),碎片也会进入停止状态,对用户完全不可见,有可能被系统回收销毁
碎片总是依附于活动,活动销毁,碎片也会销毁,或,
通过调用FragmentManager.remove()、repalce()方法将碎片从活动中移除,事务在提交前没有调用addToBackStack(),碎片会进入销毁状态Fragment类的回调方法
一般的活动有的方法,碎片都有,还有一些附加的
onAttach():当碎片和活动建立关联时onCreateVIEw():碎片创建视图,加载布局时调用onActivityCreate():确保与碎片关联的活动一定已经创建完毕的时候调用onDestroyVIEw():与碎片关联的试图被移除的时候调用onDetach():碎片与活动解除关联时使用生命周期
添加一个碎片
|
onAttach
|
onCreat
|
onCreateVIEw
|
onActivityCreate
|
onStart
|
onResume
|
碎片激活
|
onPause
|
onStop
|
onDestroyVIEw
|
onDestroy
|
onDetach
|
碎片已销毁
判断时是使用单页还是双页模式,借助限定符QualifIErs
屏幕大小 | 描述 |
---|---|
small | 小屏幕 |
normal | 中等屏幕 |
large | 大屏幕 |
xlarge | 超大屏幕 |
分辨率 | 描述 |
---|---|
ldpi | 低分辨率120dpi以下 |
mdpi | 中等分辨率120dpi - 160dpi |
hdpi | 高分辨率160dpi - 240dpi |
xhdpi | 超高分辨率240dpi - 320dpi |
xxhdpi | 超超高分辨率320dpi - 480dpi |
方向 | 描述 |
---|---|
land | 横屏 |
port | 竖屏 |
手动选择布局
最小宽度限定符运行对屏幕的宽度指定一个最小值,以dp为单位,作为临界点
例如新建一个layout-sw600dp的文件夹,说明在屏幕宽度大于600dp的设备上,会加载该文件夹下的文件,否则加载默认的layout下的布局文件
新建新闻类News
public class News { private String Title; private String content; public String getTitle() { return Title; } public String getContent() { return content; } public voID setTitle(String Title) { this.Title = Title; } public voID setContent(String content) { this.content = content; }}
News中,Title标题,content内容
新建布局文件news_content_frag.xml,作为新闻内容布局
<?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:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" androID:visibility="invisible" androID:ID="@+ID/visibility_layout"> <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:padding="10dp" androID:textSize="25sp" androID:ID="@+ID/news_Title"/> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="1dp" androID:background="#000"/> <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:padding="15dp" androID:textSize="18sp" androID:ID="@+ID/news_content"/> </linearLayout> <VIEw androID:layout_wIDth="1dp" androID:layout_height="match_parent" androID:layout_alignParentleft="true" androID:background="#000"/></relativeLayout>
黑色横线是用VIEw实现的,颜色设置为黑色,宽或高设置为1dp
新建一个NewsContentFragment类,继承自Fragment
public class NewsContentFragment extends Fragment { private VIEw vIEw; @Nullable @OverrIDe public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) { vIEw = inflater.inflate(R.layout.news_content_frag, container, false); return vIEw; } public voID refresh(String newsTitle, String newsContent) { VIEw visibilityLayout = vIEw.findVIEwByID(R.ID.visibility_layout); visibilityLayout.setVisibility(VIEw.VISIBLE); TextVIEw newsTitleText = (TextVIEw) vIEw.findVIEwByID(R.ID.news_Title); TextVIEw newsContentText = (TextVIEw) vIEw.findVIEwByID(R.ID.news_content); newsTitleText.setText(newsTitle); newsContentText.setText(newsContent); }}
加载的是news_content_frag.xml布局,refresh方法是每次点击后将新闻的标题和内容显示在fragment中。
以上都是双页模式中使用的内容
创建一个新的活动NewsContentActivity,布局名为news_content.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <fragment androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:name="org.nuaa.fragmentbestpractice.NewsContentFragment" androID:ID="@+ID/news_content_fragment"/></linearLayout>
这里直接复用NewsContentFragment
修改NewsContentActivity代码
public class NewsContentActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.news_content); String newsTitle = getIntent().getStringExtra("news_Title"); String newsContent = getIntent().getStringExtra("news_content"); NewsContentFragment newsContentFragment = (NewsContentFragment) getSupportFragmentManager().findFragmentByID(R.ID.news_content_fragment); newsContentFragment.refresh(newsTitle, newsContent); } public static voID actionStart(Context context, String newsTitle, String newsContent) { Intent intent = new Intent(context, NewsContentActivity.class); intent.putExtra("news_Title", newsTitle); intent.putExtra("news_content", newsContent); context.startActivity(intent); }}
这是在小屏手机应用的活动,actionStart是static方法,用于启动活动。onCreate中初始化fragment,使用getSupportFragmentManager获取fragment,再调用refrash。
创建用于显示新闻列表的布局news_Title_frag.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <androIDx.recyclervIEw.Widget.RecyclerVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:ID="@+ID/news_Title_recycler_vIEw"/></linearLayout>
还有子项布局news_item.xml
<?xml version="1.0" enCoding="utf-8"?><TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/news_Title" androID:maxlines="1" androID:ellipsize="end" androID:textSize="18sp" androID:paddingleft="10dp" androID:paddingRight="10dp" androID:paddingtop="15dp" androID:paddingBottom="15dp" />
ellipsize是设定当文本内容超出宽度时,文本缩略方式
新建NewsTitleFragment类
public class NewsTitleFragment extends Fragment { private boolean isTwoPane; class NewsAdapter extends RecyclerVIEw.Adapter<NewsAdapter.VIEwHolder> { private List<News> mNewsList; class VIEwHolder extends RecyclerVIEw.VIEwHolder { TextVIEw newsTitleText; public VIEwHolder(VIEw vIEw) { super(vIEw); newsTitleText = (TextVIEw) vIEw.findVIEwByID(R.ID.news_Title); } } public NewsAdapter(List<News> newsList) { mNewsList = newsList; } @NonNull @OverrIDe public VIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup parent, int vIEwType) { VIEw vIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false); final VIEwHolder holder = new VIEwHolder(vIEw); vIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { News news = mNewsList.get(holder.getAdapterposition()); if (isTwoPane) { NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentByID(R.ID.news_content_fragment); newsContentFragment.refresh(news.getTitle(), news.getContent()); } else { NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent()); } } }); return holder; } @OverrIDe public voID onBindVIEwHolder(@NonNull VIEwHolder holder, int position) { News news = mNewsList.get(position); holder.newsTitleText.setText(news.getTitle()); } @OverrIDe public int getItemCount() { return mNewsList.size(); } } @Nullable @OverrIDe public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.news_Title_frag, container ,false); RecyclerVIEw newsTitleRecyclerVIEw = (RecyclerVIEw) vIEw.findVIEwByID(R.ID.news_Title_recycler_vIEw); linearlayoutmanager layoutManager = new linearlayoutmanager(getActivity()); newsTitleRecyclerVIEw.setLayoutManager(layoutManager); NewsAdapter adapter = new NewsAdapter(getNews()); newsTitleRecyclerVIEw.setAdapter(adapter); return vIEw; } private List<News> getNews() { List<News> newsList = new ArrayList<>(); for (int i = 1; i <= 50; i++) { News news = new News(); news.setTitle("This is news Title" + i); news.setContent(getRandomLengthContent("This is news content " + i + ".")); newsList.add(news); } return newsList; } private String getRandomLengthContent(String content) { Random random = new Random(); int length = random.nextInt(20) + 1; StringBuilder builder = new StringBuilder(); for (int i = 0; i < length; i++) { builder.append(content); } return builder.toString(); } @OverrIDe public voID onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (getActivity().findVIEwByID(R.ID.news_content_layout) != null) { isTwoPane = true; } else { isTwoPane = false; } }}
修改layout/activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/news_Title_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <fragment androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:name="org.nuaa.fragmentbestpractice.NewsTitleFragment" androID:ID="@+ID/news_Title_fragment"/></FrameLayout>
再新建layout_large/activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <fragment androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:name="org.nuaa.fragmentbestpractice.NewsTitleFragment" androID:ID="@+ID/news_Title_fragment"/> <FrameLayout androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="3" androID:ID="@+ID/news_content_layout"> <fragment androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:name="org.nuaa.fragmentbestpractice.NewsContentFragment" androID:ID="@+ID/news_content_fragment"/> </FrameLayout></linearLayout>
在onActivityCreated方法中,使用if(getActivity().findVIEwByID(R.ID.news_content.layout) != null)
即可判断是双页还是单页
AndroID有一套完整的API,可以自由发送和接收广播
发送使用Intent,接收使用接收器broadcast Receiver
广播有两种类型:标准广播和有序广播
注册的方式有两种:
动态注册:在代码中注册静态注册:AndroIDManifest.xml注册动态注册监听网络变化广播接收器,继承broadcast类,并重写onReceive方法,具体接收到广播后的处理逻辑就写在里面
public class MainActivity extends AppCompatActivity { private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); intentFilter = new IntentFilter(); intentFilter.addAction("androID.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver, intentFilter); } class NetworkChangeReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context, Intent intent) { Log.d("TAG", "changed"); ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); Network network = connectivityManager.getActiveNetwork(); if (network == null) { Toast.makeText(context, "network is unavailable", Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, "network is available", Toast.LENGTH_LONG).show(); } } } @OverrIDe protected voID onDestroy() { super.onDestroy(); unregisterReceiver(networkChangeReceiver); }}
创建NetworkChangeReceiver类
在onCreate中新建IntentFilter实例,添加一个值androID.net.conn.CONNECTIVITY_CHANGE
的action,因为当网络状态变化时,会发出这么一个广播,这里注册的意思就是接收这个广播registerReceiver(networkChangeReceiver, intentFilter)
还有在onDestroy方法中取消注册。
还有获取网络状态需要申请权限,在AndroIDManifest.xml文件中添加<uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE"/>
动态注册很灵活,但是程序必须启动后才能接收到广播,静态注册可以在未启动就能接受广播
新建一个BootCompleteReceiver,在AndroIDManifest.xml文件中注册,如果是用AndroID Studio快捷方式新建的类已经注册好了,在Application标签下
<receiver androID:name=".BootCompleteReceiver" androID:enabled="true" androID:exported="true"> <intent-filter> <action androID:name="androID.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
还需要声明权限<uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED"/>
这样就可以自启动了
在广播的onReceive方法中不要写太多的逻辑和耗时的 *** 作,不允许开启线程,运行较长时间没结束会报错
所以广播的作用大多是创建一条通知栏,开启一个服务等
定义一个接收器MybroadcastReceiver
public class MybroadcastReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context, Intent intent) { Toast.makeText(context, "received in MybroadcastReceiver", Toast.LENGTH_LONG).show(); }}
注册
<receiver androID:name=".MybroadcastReceiver" androID:enabled="true" androID:exported="true"> <intent-filter> <action androID:name="org.nuaa.broadcasttest.MY_broADCAST"/> </intent-filter> </receiver>
主活动中添加一个按钮,点击发送广播
button button = (button) findVIEwByID(R.ID.send); button.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent("org.nuaa.broadcasttest.MY_broADCAST"); sendbroadcast(intent); } });
注意,在高版本的AndroID系统中,静态广播诸多限制,建议改成动态注册广播,或者加上intent.setComponent(new Componentname("pckname", "receiver path"));
,不过这样的话就只能针对一个广播接收器发送了,目前暂不知道解决方案
改成sendOrderbroadcast,并且在静态注册处添加优先级<intent-filer priority="100">
在接收器代码添加abortbroadcast()
可以截断广播
似乎没有该机制了,暂时不写
广播实践 -- 强制下线功能一个ActivityCollector管理所有活动
public class ActivityCollector { public static List<Activity> activitIEs = new ArrayList<>(); public static voID addActivity(Activity activity) { activitIEs.add(activity); } public static voID removeActivity(Activity activity) { activitIEs.remove(activity); } public static voID finishAll() { for (Activity activity : activitIEs) { if (!activity.isFinishing()) { activity.finish();; } } activitIEs.clear(); }}
创建BaseActivity基类作为所有活动的父类
public class BaseActivity extends AppCompatActivity { private ForceOfflineReceiver receiver; @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @OverrIDe protected voID onDestroy() { super.onDestroy(); ActivityCollector.removeActivity(this); } @OverrIDe protected voID onResume() { super.onResume(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("org.nuaa.broadcasttest2.FORCE_OFFliNE"); receiver = new ForceOfflineReceiver(); registerReceiver(receiver, intentFilter); } @OverrIDe protected voID onPause() { super.onPause(); if (receiver != null) { unregisterReceiver(receiver); receiver = null; } } class ForceOfflineReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(final Context context, Intent intent) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Warning"); builder.setMessage("You are forced to be offline"); builder.setCancelable(false); builder.setPositivebutton("OK", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { ActivityCollector.finishAll(); Intent intent = new Intent(context, LoginActivity.class); context.startActivity(intent); } }); builder.show(); } }}
在基类中创建强制下线广播接收器,onReceive方法中代码逻辑是d出一个对话框,setCacleable为false即不可取消,OK按钮点击后关闭所有活动,并重新启动登录活动
新建LoginActivity类,继承BaseActivity
public class LoginActivity extends BaseActivity { private EditText accountEdit; private EditText passwdEdit; private button login; @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); accountEdit = (EditText) findVIEwByID(R.ID.account); passwdEdit = (EditText) findVIEwByID(R.ID.passwd); login = (button) findVIEwByID(R.ID.login); login.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { String account = accountEdit.getText().toString(); String passwd = passwdEdit.getText().toString(); if (account.equals("admin") && passwd.equals("123")) { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "error", Toast.LENGTH_SHORT).show(); } } }); }}
界面布局如下
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <linearLayout androID:layout_marginleft="15dp" androID:layout_marginRight="15dp" androID:layout_wIDth="match_parent" androID:layout_height="60dp" androID:orIEntation="horizontal"> <TextVIEw androID:layout_wIDth="90dp" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:textSize="18sp" androID:text="Account:"/> <EditText androID:ID="@+ID/account" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:layout_gravity="center_vertical"/> </linearLayout> <linearLayout androID:layout_marginleft="15dp" androID:layout_marginRight="15dp" androID:layout_wIDth="match_parent" androID:layout_height="60dp" androID:orIEntation="horizontal"> <TextVIEw androID:layout_wIDth="90dp" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:textSize="18sp" androID:text="Password:"/> <EditText androID:ID="@+ID/passwd" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:hint="textPassword" androID:layout_gravity="center_vertical"/> </linearLayout> <button androID:ID="@+ID/login" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:text="@string/action_sign_in" /></linearLayout>
主界面只有一个按钮,点击强制下线
public class MainActivity extends BaseActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); button offline = (button) findVIEwByID(R.ID.offline); offline.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent("org.nuaa.broadcasttest2.FORCE_OFFliNE"); sendbroadcast(intent); } }); }}
最后把主活动改成LoginActivity
<intent-filter> <action androID:name="androID.intent.action.MAIN"/> <@R_301_4602@ androID:name="androID.intent.@R_301_4602@.LAUNCHER"/> </intent-filter>
总结 以上是内存溢出为你收集整理的android入门学习 -- 3 -- 碎片Fragment&广播Broadcast全部内容,希望文章能够帮你解决android入门学习 -- 3 -- 碎片Fragment&广播Broadcast所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)