android可行代码小记

来源:互联网 发布:nginx 文件下载服务器 编辑:程序博客网 时间:2024/05/01 05:13
创建sdcard:在cmd命令行进入android-sdk安装目录下的tools文件夹下,再输入类似代码:mksdcard -l sdcard 200M d:/android/tools/mysdcard/sdcard.img
一.activity中代码
获取sdcard路径:android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
1.oncreat方法部分可重用代码:
记得先声明private Button startGameButton = null;
setContentView(R.layout.main);//设置布局
startGameButton = (Button)findViewById(R.id.startGameButton);//设置按钮组件
startGameButton.setOnClickListener(new startGameButtonListener());//设置监听
class startGameButtonListener implements OnClickListener{//编写触摸监听类
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Intent intent = new Intent();  //调用别的activity方法
   intent.putExtra("startGameMode", "1");
//第一个activity-StartChildToyRead转为第二个activity-ChildToyReadActivity
   intent.setClass(StartChildToyRead.this, ChildToyReadActivity.class); 
   StartChildToyRead.this.startActivity(intent);
  }
 }
2.消息机制
public void creatMessage(String message){
     System.out.println("得到消息:"+message);
     Looper looper = Looper.myLooper();
  MyHandler mHandler = new MyHandler(looper);
     mHandler.removeMessages(0);
     Message m = mHandler.obtainMessage(1, 1, 1, message);
     mHandler.sendMessage(m);
    }
private class MyHandler extends Handler {
  public MyHandler(Looper looper) {
   super(looper);
  }
  @Override
  public void handleMessage(Message msg) { //编写一系列接受到消息msg后的处理
   // TODO Auto-generated method stub
   super.handleMessage(msg);
   String m = msg.obj.toString();
   if (m.equals("begin")) {
    conf = new Configure();
    interf = new HttpInterface();
    initDataBindings init = new initDataBindings(DlgDownload.this,
      interf);
    init.start();
   } else if (m.equals("end")) {
    edlButton.setVisibility(View.VISIBLE);
   }
  }
 }
3.音频播放
public static void Play(String path) throws Exception{
     MediaPlayer mp = null;
  mp = new MediaPlayer();
  mp.setDataSource(path);
  mp.prepare();
  mp.start();
  mp.setOnCompletionListener(new OnCompletionListener(){     此处可用消息机制,这样后面的while循环可以省略
            @Override  
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
         });
  while(mp.isPlaying()){}
    }
4.AlertDialog.Builder组件使用方法
AlertDialog.Builder builder = new AlertDialog.Builder(类名.this);
builder.setTitle("警告").setIcon(R.drawable.ic_launcher).
     setCancelable(false).setMessage("您选择的路径不对,缺少相关文件").setNegativeButton("确定", new OKButton());
     dialog = builder.show();
 
class OKButton implements DialogInterface.OnClickListener{
  @Override
  public void onClick(DialogInterface dialog, int which) {
   // TODO Auto-generated method stub
   dialog.dismiss();
  } 
 }
5.关闭AlertDialog.Builder方法
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Dialog dialog = builder.show();
dialog.dismiss();
二.布局文件一些代码
1.组件不可见
在布局文件中添加:android:visibility="gone"
要显示,在activity中运行:组件名.setVisibility(View.VISIBLE);
2.组件的排列
android:orientation="horizontal"//横向
android:orientation="vertical"//纵向
3.组件的标示
android:checked="true"设置RadioButton被选中
android:layout_width="30dp"设置宽度,也即对EditText定长度
android:maxLength="4"用于设置EditText输入字符长度
<requestFocus />强制在EditText中显示输入光标
三.AndroidManifest.xml中相关内容
1.文件下载到sdcard,及读写sdcard文件,记得在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.申明主activity次activity实例
<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity                    //主activity
            android:name=".StartChildToyRead"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity             //次activity
            android:screenOrientation="landscape"
            android:name=".ttsDemo"
            android:label="@string/tts" 
            android:theme="@android:style/Theme.Dialog"/>   //小窗口显示activity
        <activity             //次activity
            android:name=".DownLoadStory"
            android:label="@string/sg" 
        />
     </application>
四.java调试用语句:
写日志:例如hwano.util.file.Log.printError("text","c:\\jilu\\text.txt");
原创粉丝点击