android 设置activity标题

  • 内容
  • 评论
  • 相关

java文件

package com.example.settitle;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	private boolean isFinished;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setTitle("窗口标题");
	}
	
	public void onClickSetTitle(View view){
		setTitle("新窗口标题");
		setTitleColor(123456);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.d("MainActivity","onPostCreate");
		super.onPostCreate(savedInstanceState);
	}

	@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
	protected void onTitleChanged(CharSequence title, int color) {
		// TODO Auto-generated method stub
		super.onTitleChanged(title, color);
		Log.d("MainActivity","onTitleChangedTitle" + title);
		Log.d("MainActivity","onTitleChangedColor" + color);
	}

/*	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Log.d("onKeyDown",String.valueOf(keyCode));
		Log.d("onKeyDown:repeatCount",String.valueOf(event.getRepeatCount()));
		if(event.getRepeatCount() == 100){
			Toast.makeText(this, "嘻嘻,别总按伦家捏,放开捏", Toast.LENGTH_LONG).show();
		}
		if(keyCode == KeyEvent.KEYCODE_MENU){
//			Toast.makeText(this, "嘻嘻,别总按伦家捏,放开捏", Toast.LENGTH_LONG).show();
			Log.d("onKeyDown","You Press MENU down");
		}
		return super.onKeyDown(keyCode, event);
	}*/

/*	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction() == KeyEvent.ACTION_DOWN){
			Log.d("displatchKeyEvent","down");
		}else if(event.getAction() == KeyEvent.ACTION_UP){
			Log.d("displatchKeyEvent","up");
		}
		return super.dispatchKeyEvent(event);
	}*/

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Log.d("onBackPressed","OK");
		super.onBackPressed();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		event.startTracking();
		return true;
//		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "窗口已经恢复", Toast.LENGTH_LONG).show();
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		if(!isFinished){
			Toast.makeText(this, "窗口已经在后台运行(按了HOME键)", Toast.LENGTH_LONG).show();
		}
		super.onStop();
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		isFinished = true;
		super.finish();
	}

	@Override
	public boolean onKeyLongPress(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Log.d("onKeyLongPress","onKeyLongPress_KeyCode:"+keyCode);
		return super.onKeyLongPress(keyCode, event);
	}

/*	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_MENU){
			Log.d("onKeyUp","You make MENU up");
		}
		return super.onKeyUp(keyCode, event);
	}*/
	
}

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
	
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickSetTitle"
        android:text="设置窗口标题"
        />

</LinearLayout>

setTitleColor虽然可以调用,但是无法修改窗口标题文本颜色,这应该是android的一个bug捏,不过我们可以使用其他更灵活的方法来设置。

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注