本文共 5937 字,大约阅读时间需要 19 分钟。
更改文字颜色
文字色:
TextView.setTextColor(Color.***);
背景色:
Resources resources = getBaseContext().getResources();Drawable HippoDrawable = resources.getDrawable(R.drawable.white);TextView.setBackgroundDrawable(HippoDrawable);
获取手机分辨率
import android.util.DisplayMetricsDisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);String x,y;x=dm.widthPixels;y=dm.heightPixels;
页面转换
在Android应用中,是通过setContentView来实现页面的转换处理的,也就是在不同的layout之间进行切换。
setContentView(R.layout.****);
例子:
package com.example.test4;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.support.v4.app.NavUtils;public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b1=(Button)findViewById(R.id.button1); b1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub jumpToMylayout(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void jumpToMylayout(){ setContentView(R.layout.mylayout); Button b2 = (Button)findViewById(R.id.button1); b2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub jumpToLayout(); } }); } public void jumpToLayout(){ setContentView(R.layout.activity_main); Button b1=(Button)findViewById(R.id.button1); b1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub jumpToMylayout(); } }); }}
调用其他Activity
与上面在不同layout之间转换不同,这里我们需要移交主权到另外一个Activity。
//一个Activity Intent intent=new Intent(); intent.setClass(MainActivity.this,replace.class);startActivity(intent);MainActivity.this.finish();//另外一个ActivityIntent intent=new Intent();intent.setClass(replace.this, MainActivity.class);startActivity(intent);replace.this.finish();
不同Activity之间的数据转换
利用Android.os.Bundle对象封装数据的能力,将欲传递的数据或参数,通过Bundle来传递不同Intent之间的数据。
发送:
Intent intent=new Intent();intent.setClass(MainActivity.this,display.class); Bundle bundle = new Bundle();bundle.putString("num",et1.getText().toString());bundle.putString("name",et2.getText().toString()); intent.putExtras(bundle); startActivity(intent);
接收:
Bundle bundle = this.getIntent().getExtras();String num = bundle.getString("num");String name = bundle.getString("name");
返回数据到前一个Activity
唤起一个Activity:
startActivityForResult(intent,0)
重载onActivityResult:
protected void onActivityResult(int requestCode,int resultCode,Intent data){
switch(resultCode){
case RESULT_OK:
Bundle bundle=data.getExtras();
//code there break; default: break;}
}
返回数据:
input.this.setResult(RESULT_OK, intent);
部分代码:
//main_activitypackage com.example.test7;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.support.v4.app.NavUtils;public class MainActivity extends Activity { TextView tx; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt=(Button)findViewById(R.id.button1); tx=(TextView)findViewById(R.id.textView1); bt.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, input.class); startActivityForResult(intent,0); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ switch(resultCode){ case RESULT_OK: Bundle bundle=data.getExtras(); String str=bundle.getString("str").toString(); tx.setText(str); break; default: break; } } }
/** * input.java */package com.example.test7;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class input extends Activity { Intent intent; Bundle bundle; EditText et; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.input); Button bt=(Button)findViewById(R.id.button1); et=(EditText)findViewById(R.id.editText1); intent = this.getIntent(); bundle= new Bundle(); bt.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub bundle.putString("str", et.getText().toString()); intent.putExtras(bundle); input.this.setResult(RESULT_OK, intent); input.this.finish(); } }); }}
本文 由 创作,采用 进行许可。欢迎转载,请注明出处: 转载自: