Android 笔记 5
来源:互联网 发布:金蝶软件苏州分公司 编辑:程序博客网 时间:2024/06/10 05:06
目录
这几天写的一些布局,全都混在一起了,懒得整理
package com.example.linj.fragmentapplication.activity;import android.app.AlertDialog;import android.app.DatePickerDialog;import android.app.Dialog;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TimePickerDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.graphics.drawable.Drawable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.Html;import android.text.Spanned;import android.view.Gravity;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.Button;import android.widget.DatePicker;import android.widget.PopupWindow;import android.widget.RemoteViews;import android.widget.TextView;import android.widget.TimePicker;import android.widget.Toast;import com.example.linj.fragmentapplication.R;import com.example.linj.fragmentapplication.fragment.AnotherRightFragment;import java.text.SimpleDateFormat;import java.util.Calendar;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button; private Button mButtonToast; private Button mButtonMyToast; private Button mButtonDialog; private String[] mData = {"9", "1", "2", "3", "5", "6", "1", "2", "3", "5", "6"}; private String[] mSex = {"男", "女"}; private String sex = ""; private boolean[] mIsChecked = new boolean[mData.length]; private StringBuffer num; private Button mButtonMyDialog; private Dialog myDialog; private Button mButtonTime; private Calendar calendar; private Button mButtonHour; private Button mButtonPopupWindow; private PopupWindow popupWindow; private Button mButtonNotification; private Button mButtonCancel; private NotificationManager notificationManager; private Button mButtonRemote; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonToast = (Button) findViewById(R.id.button_toast); mButtonToast.setOnClickListener(this); button = (Button) findViewById(R.id.button_left_fragment); button.setOnClickListener(this); mButtonMyToast = (Button) findViewById(R.id.button_my_toast); mButtonMyToast.setOnClickListener(this); mButtonDialog = (Button) findViewById(R.id.button_dialog); mButtonDialog.setOnClickListener(this); mButtonMyDialog = (Button) findViewById(R.id.button_my_dialog); mButtonMyDialog.setOnClickListener(this); mButtonTime = (Button) findViewById(R.id.button_time); mButtonTime.setOnClickListener(this); mButtonHour = (Button) findViewById(R.id.button_hour); mButtonHour.setOnClickListener(this); mButtonPopupWindow = (Button) findViewById(R.id.button_popup); mButtonPopupWindow.setOnClickListener(this); mButtonNotification = (Button) findViewById(R.id.button_notification); mButtonNotification.setOnClickListener(this); mButtonCancel = (Button) findViewById(R.id.button_notification_cancel); mButtonCancel.setOnClickListener(this); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mButtonRemote = (Button) findViewById(R.id.button_notification_remote); mButtonRemote.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_notification_remote: RemoteViews views = new RemoteViews(getPackageName(),R.layout.remote_view_notification); Intent intent3 = new Intent(getApplicationContext(), MainActivity.class); PendingIntent pendingIntent3 = PendingIntent.getActivity(getApplicationContext(),1,intent3,PendingIntent.FLAG_ONE_SHOT); Notification notification3 = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true).setContentIntent(pendingIntent3).setContent(views).build(); notificationManager.notify(2, notification3); break; case R.id.button_notification: Notification notification = new Notification(); notification.icon = R.mipmap.ic_launcher; notification.tickerText = "你是一个通知,通知下一个活动";// notification.defaults = Notification.DEFAULT_ALL;//使用震动得添加权限。。。。。 notification.flags = Notification.FLAG_AUTO_CANCEL; notification.when = System.currentTimeMillis(); Intent intent = new Intent(getApplicationContext(), MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),1,intent,PendingIntent.FLAG_ONE_SHOT); notification.setLatestEventInfo(getApplicationContext(),"通知标题", "通知内容",pendingIntent); notificationManager.notify(1, notification); break; case R.id.button_notification_cancel: Intent intent1 = new Intent(getApplicationContext(), MainActivity.class); PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(),1,intent1,PendingIntent.FLAG_ONE_SHOT); Notification notification1 = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher) .setTicker("你是一个通知").setContentTitle("通知标题").setContentText("你的手机欠费了").setWhen(System.currentTimeMillis()) .setContentInfo("信息?").setAutoCancel(true).setContentIntent(pendingIntent1).build(); notificationManager.notify(2, notification1); break; case R.id.button_popup: popupWindow = new PopupWindow(MainActivity.this); View popView = getLayoutInflater().inflate(R.layout.my_popup, null); popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setContentView(popView); popupWindow.setOutsideTouchable(true); popupWindow.showAsDropDown(mButtonMyDialog); break; case R.id.button_hour: calendar = Calendar.getInstance(); TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) {// calendar.set(hourOfDay,minute); calendar.set(Calendar.HOUR, hourOfDay); calendar.set(Calendar.MINUTE, minute); SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH:mm"); Toast.makeText(getApplicationContext(), format.format(calendar.getTime()), Toast.LENGTH_SHORT).show(); } }, calendar.get(calendar.HOUR), calendar.get(calendar.MINUTE), true); timePickerDialog.show(); break; case R.id.button_time: calendar = Calendar.getInstance(); DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { calendar.set(year, monthOfYear, dayOfMonth); SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); Toast.makeText(getApplicationContext(), format.format(calendar.getTime()), Toast.LENGTH_SHORT).show(); } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_YEAR)); datePickerDialog.show(); break; case R.id.button_my_dialog: myDialog = new Dialog(this, R.style.NoDialogTitle); LayoutInflater inflater = getLayoutInflater(); View dialogView = inflater.inflate(R.layout.my_dialog_layout, null); TextView textViewTitle = (TextView) dialogView.findViewById(R.id.dialog_title); TextView textViewMessage = (TextView) dialogView.findViewById(R.id.dialog_message); Button buttonCancel = (Button) dialogView.findViewById(R.id.button_cancel); Button buttonOk = (Button) dialogView.findViewById(R.id.button_ok); textViewTitle.setText("new title"); textViewMessage.setText("new content"); buttonCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myDialog.dismiss(); Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show(); } }); buttonOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myDialog.dismiss(); Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show(); } }); myDialog.setCancelable(false);// myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); myDialog.setContentView(dialogView); myDialog.show(); break; case R.id.button_dialog: AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher).setTitle("选择").setCancelable(false);// builder.setMessage("被迫下线"); builder.setMultiChoiceItems(mData, mIsChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { mIsChecked[which] = isChecked; } });// builder.setSingleChoiceItems(mSex, 2, new DialogInterface.OnClickListener() {// @Override// public void onClick(DialogInterface dialog, int which) {// sex = mSex[which];// Toast.makeText(getApplicationContext(), "性别"+sex, Toast.LENGTH_SHORT).show();// }// });// builder.setItems(mData, new DialogInterface.OnClickListener() {// @Override// public void onClick(DialogInterface dialog, int which) {// Toast.makeText(getApplicationContext(), "忽略第"+(which+1)+"个数据", Toast.LENGTH_SHORT).show();// }// }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "忽略", Toast.LENGTH_SHORT).show(); } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { num = new StringBuffer(); for (int i = 0; i < mIsChecked.length; i++) { if (mIsChecked[i]) { num.append(mData[i]); } } if (num.length() == 0) { } else { mButtonDialog.setText(num); }// if (sex.equals("")){// } else {// mButtonDialog.setText(sex);// Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();// } } });// AlertDialog dialog = builder.create();// dialog.show(); builder.show(); break; case R.id.button_my_toast: Toast toast1 = new Toast(getApplicationContext()); LayoutInflater layoutInflater = getLayoutInflater(); View view = layoutInflater.inflate(R.layout.my_toast_layout, null); TextView textView = (TextView) view.findViewById(R.id.text_view_toast); textView.setText("我是toast的标题"); toast1.setView(view); toast1.setDuration(Toast.LENGTH_SHORT); toast1.show(); break; case R.id.button_toast: Toast toast2 = Toast.makeText(getApplicationContext(), "abc", Toast.LENGTH_SHORT); Spanned spanned = Html.fromHtml("<img src=''/>我是一个<font color>Toast</font>", new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; } }, null); toast2.setText(spanned); toast2.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL, 0, 0); toast2.show(); break; case R.id.button_left_fragment: AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.addToBackStack(null); transaction.commit(); break; default: break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); return true; } } return super.onKeyDown(keyCode, event); }}
总结
以后养成好习惯,写注释, 分开写,整理规划好
0 0
- Android学习笔记5
- android 笔记5
- android学习笔记5
- android学习笔记(5)
- Android学习笔记5
- Android笔记(5)
- android笔记5-listView
- Android 笔记 5
- Android笔记5
- [android]笔记5-TableLayout
- Android 学习笔记(5) --tablelayout
- android学习笔记NO.5
- android学习笔记(5)
- android笔记
- android笔记
- android笔记
- Android笔记
- Android笔记
- 《暗时间》读书笔记
- C语言基础知识之(十六):结构体和指针
- HDU 5154 Harry and Magical Computer(找环)
- Hadoop系列之单机版安装
- POST请求的两种格式
- Android 笔记 5
- 黑马程序员----------------Java基础-----------------IO流学习小结
- LeetCode题解:Valid Parentheses
- java
- 百度面试题 最长公共子串-动态规划法
- 数据库设计(2)生命周期
- 黑马程序员_IO流
- 济宁惠普第一个文章
- Struts2中struts.xml文件的加载过程