/*** In the Activity Now ***/
Int IntValueB, IntValue;
String StringValueB, StringValue;
...
Intent intentName = new Intent(ActivityNow.this, ActivityToChange.class);
// Method 1
Bundle extra = new Bundle();
extra.putString("varStrB", StringValueB);
extra.putInt("varIntB", StringValueB);
...
intentName.putExtra(extra); //
// Method 2
intentName.putExtra("varStr", StringValue);
intentName.putExtra("varInt", IntValue);
... //
startActivity(intentName);
/*** In the Activity to Change ***/
Int IntValueB, IntValue;
String StringValueB, StringValue;
...
Bundle extra = getIntent().getExtras();
StringValueB = extra.getString("varStrB");
IntValueB = extra.getInt("varIntB");
StringValue = extra.getString("varStr");
IntValue = extra.getInt("varInt");
...
Broadcast (Default & Custom Actions)
/*** Send Broadcast ***/
public class TheActivity extends Activity {
static Intent mBroadcastCustom, mBroadcastDefault;
...
protected void onCreate(Bundle savedInstanceState) {
mBroadcastCustom =
new Intent("sowhat.practice.provider.ACTION_CLEAN");
mBroadcastDefault =
new Intent(Intent.ACTION_ALL_APPS);
...
}
// Somewhere in the class (could be in onCreate also)
sendBroadcast(mBroadcastCustom);
sendBroadcast(mBroadcastDefault);
}
/*** Receive Broadcast ***/
public class TheOtherActivity extends Activity{
// Setup for custom broadcast
private static IntentFilter mFilterCustom =
new IntentFilter("sowhat.practice.provider.ACTION_CLEAN");
private BroadcastReceiver mReceiverCustom =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
...
}
};
// Setup for default broadcast
private static IntentFilter mFilterDefault =
new IntentFilter("Intent.ACTION_ALL_APPS");
private BroadcastReceiver mReceiverDefault =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
...
}
};
protected void onCreate(Bundle savedInstanceState) {
// "Turn on" the broadcast receiver
registerReceiver(mReceiverCustom, mFilterCustom);
registerReceiver(mReceiverDefault, mFilterDefault);
...
}
}
onClickListener
public class MainActivity extends Activity
implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Method 1
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
...
}
});
// Method 2
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
// Method 3
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new Image_Listener(this));
}
// Method 2
public void onClick(View v){
...
}
// Method 3
class Image_Listener implements OnClickListener {
private MainActivity activity;
public Image_Listener(MainActivity activity) {
this.activity = activity;
}
@Override
public void onClick(View v) {
...
}
}
...
}
ps. The activity should implements View.OnClickListener if and only if using method 3.
Toast
import android.widget.Toast;
...
// In activity
Toast.makeText(getApplicationContext(), <string to show>, Toast.LENGTH_SHORT).show();
// In fragment
Toast.makeText(getActivity(), <string to show>, Toast.LENGTH_SHORT).show();
- Hide input method (keyboard)
// In fragment
View view = inflater.inflate(R.layout.<layout_name>, null);
InputMethodManager imm = (InputMethodManager)
getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow
(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
- Get class / package name
// Not in the activity class
String className = <class_name>.class.getName();
// In the activity class
String className = this.getClass().getName();
String packageName = this.getPackageName();
- Parameters for adding fullscreen view by WindowManager
WinowManager.LayoutParams mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR,
PixelFormat.OPAQUE);
o. FLAG_LAYOUT_IN_SCREEN + FLAG_LAYOUT_INSET_DECOR
真正全螢幕。
o. FLAG_FULLSCREEN
不包含 status bar 位置。
若後方 activity 隱藏 status bar,會在 status bar 位置看穿後方內容。
o. FLAG_FORCE_NOT_FULLSCREEN
不包含 status bar 位置。
即使後方 activity 隱藏 status bar,也會強迫顯示,但 status bar 顏色依後方 activity 而定。
- Set portrait or landscape for view, disable auto-rotation
WinowManager.LayoutParams params;
params = new WindowManager.LayoutParams(...);
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
or
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
REF: http://stackoverflow.com/questions/14587085/how-can-i-globally-force-screen-orientation-in-android
- 設定鍵盤及其相關行為
< 方法 1 - for activity >
在 AndroidManifest.xml 中的 activity 宣告 android:windowSoftInputMode ,
設定進入時是否顯示鍵盤 (stateXXX) 以及鍵盤出現時是否調整 layout (adjustXXX)
REF:
http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
< 方法 2 - for alert dialog>
alertDialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN |
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
REF:
http://stackoverflow.com/questions/5622202/how-to-resize-alertdialog-on-the-keyboard-displayhttp://developer.android.com/reference/android/view/WindowManager.LayoutParams.html
- Parameters for adding fullscreen view by WindowManager
// In xml
<TextView
android:id="@+id/status_bar_blank"
android:layout_width="match_parent"
android:layout_height="0dp" />
// In java
TextView statusBarBlank = (TextView) mFloatView.findViewById(R.id.status_bar_blank);
statusBarBlank.getLayoutParams().height = getStatusBarHeight();
statusBarBlank.setBackgroundColor(Color.BLACK);
private int getStatusBarHeight() {
final Display display = mWindowManager.getDefaultDisplay();
if (display != null && display.getDisplayId() != Display.DEFAULT_DISPLAY) {
return 0;
}
int h = 0;
int resourceId = mContext.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
h = mContext.getResources().getDimensionPixelSize(resourceId);
}
return h;
}
- Underline TextView
TextView textView = (TextView) view_name.findViewById(R.id.textView_name);
textView.getPaint().setUnderlineText(true);
沒有留言:
張貼留言