Android学习笔记(二)基础知识(1)
更改文字顏色
文字色:
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;
?
頁面轉(zhuǎn)換
在Android應(yīng)用中,是通過setContentView來實現(xiàn)頁面的轉(zhuǎn)換處理的,也就是在不同的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 {@Overridepublic 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();}});}@Overridepublic 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();}});} }?
調(diào)用其他Activity
?
與上面在不同layout之間轉(zhuǎn)換不同,這里我們需要移交主權(quán)到另外一個Activity。
//一個ActivityIntent intent=new Intent();
intent.setClass(MainActivity.this,replace.class); startActivity(intent); MainActivity.this.finish(); //另外一個Activity Intent intent=new Intent(); intent.setClass(replace.this, MainActivity.class); startActivity(intent); replace.this.finish();
?
不同Activity之間的數(shù)據(jù)轉(zhuǎn)換
?利用Android.os.Bundle對象封裝數(shù)據(jù)的能力,將欲傳遞的數(shù)據(jù)或參數(shù),通過Bundle來傳遞不同Intent之間的數(shù)據(jù)。
發(fā)送:
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");?
?返回數(shù)據(jù)到前一個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;
}
}
?返回數(shù)據(jù):
input.this.setResult(RESULT_OK, intent);
部分代碼:
//main_activity package 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;@Overridepublic 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 stubIntent intent = new Intent();intent.setClass(MainActivity.this, input.class);startActivityForResult(intent,0);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}@Overrideprotected 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;@Overridepublic 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 stubbundle.putString("str", et.getText().toString());intent.putExtras(bundle);input.this.setResult(RESULT_OK, intent);input.this.finish();}});} }?本文轉(zhuǎn)自cococo點點博客園博客,原文鏈接:http://www.cnblogs.com/coder2012/archive/2013/05/13/3075259.html,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的Android学习笔记(二)基础知识(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache Spark 2.2.0 中
- 下一篇: 使用Axis2来构建Web Servic