当前位置:   article > 正文

23-编码实现软件界面与通知_软件开发 页面自动提示功能的实现

软件开发 页面自动提示功能的实现

编码实现软件通知

构建新的xml

  1. package cn.itcast.codeui;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.LinearLayout;
  9. import android.widget.TextView;
  10. public class MainActivity extends Activity {
  11. /** Called when the activity is first created. */
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. LinearLayout linearLayout = new LinearLayout(this);
  16. linearLayout.setOrientation(LinearLayout.VERTICAL);
  17. TextView textView = new TextView(this);
  18. textView.setText(R.string.hello);
  19. ViewGroup.LayoutParams textViewParams = new ViewGroup.LayoutParams(
  20. ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  21. linearLayout.addView(textView, textViewParams);
  22. View partView = getPartUI(); //该部分是固定的 上面是动态的
  23. linearLayout.addView(partView);
  24. ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
  25. ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
  26. setContentView(linearLayout, layoutParams);
  27. }
  28. private View getPartUI(){
  29. LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  30. return inflater.inflate(R.layout.part, null); //由界面文件生成一个view对象,要不要挂一个父类元素
  31. }
  32. }

part.xml 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6. <EditText
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. />
  10. <Button
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="添加"
  14. />
  15. </LinearLayout>

通知

string.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, MainActivity!</string>
  4. <string name="app_name">发送通知</string>
  5. <string name="shorttitle">概要</string>
  6. <string name="title">标题</string>
  7. <string name="content">内容</string>
  8. <string name="button">发送</string>
  9. </resources>
layout.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/shorttitle"
  11. />
  12. <EditText
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:id="@+id/shorttitle"
  16. />
  17. <TextView
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:text="@string/title"
  21. />
  22. <EditText
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:id="@+id/title"
  26. />
  27. <TextView
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:text="@string/content"
  31. />
  32. <EditText
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:id="@+id/content"
  36. />
  37. <Button
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="@string/button"
  41. android:onClick="send"
  42. />
  43. </LinearLayout>
MainActivity.java
  1. package cn.itcast.notification;
  2. import android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.EditText;
  12. public class MainActivity extends Activity {
  13. private EditText shorttitleText;
  14. private EditText titleText;
  15. private EditText contentText;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. shorttitleText = (EditText) this.findViewById(R.id.shorttitle);
  21. titleText = (EditText) this.findViewById(R.id.title);
  22. contentText = (EditText) this.findViewById(R.id.content);
  23. }
  24. public void send(View v){
  25. String tickerText = shorttitleText.getText().toString();
  26. String title = titleText.getText().toString();
  27. String content = contentText.getText().toString();
  28. int icon = android.R.drawable.stat_notify_chat;
  29. Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
  30. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:194949494"));
  31. PendingIntent pendingIntent = PendingIntent.getActivity(this, 10, intent, 0);
  32. notification.setLatestEventInfo(this, title, content, pendingIntent); // pengingIntent 指点击通知时候打开那个应用
  33. notification.defaults = Notification.DEFAULT_SOUND;//声音
  34. notification.flags = Notification.FLAG_AUTO_CANCEL; //通知被点击则取消显示
  35. NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  36. manager.notify(100, notification);//100是id 自定义唯一
  37. }
  38. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.itcast.notification"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".MainActivity"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. <uses-sdk android:minSdkVersion="8" />
  16. <uses-permission android:name="android.permission.CALL_PHONE"/>
  17. </manifest>


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/241000
推荐阅读
相关标签
  

闽ICP备14008679号