管理片段
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Java
public class MainActivity extends AppCompatActivity {
private Integer con = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 方法1
// Frag1 f1 = (Frag1) fragmentManager.findFragmentById(R.id.fragment);
// 方法2
Frag1 f1 = (Frag1) fragmentManager.findFragmentByTag("tag");
Button bt = (Button) f1.getView().findViewById(R.id.button);
bt.setText("Fragmentbut");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
con = con + 1;
Toast.makeText(MainActivity.this, "Count" + con, Toast.LENGTH_SHORT).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.e560.m1014c.MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.e560.m1014c.Frag1"
android:tag="tag"
android:layout_weight="1" />
</LinearLayout>
2016年10月15日土曜日
2016年10月8日土曜日
startActivityForResult
startActivityForResult 的利用
1. 跳转新的Activity方法
public void bt1(View view){
Toast.makeText(this, "bu1", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, v2.class);
startActivityForResult(intent, 99);
}
2. 回调方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String ss = data.getStringExtra("a").toString();
((EditText) findViewById(R.id.editText1)).setText(ss);
String b = data.getStringExtra("b").toString();
String c = data.getStringExtra("c").toString();
Toast.makeText(this, b, Toast.LENGTH_SHORT).show();
this.setTitle(c);
}
3. 从新的Activity返回值的的方法
public void bu2(View view){
EditText et = (EditText) findViewById(R.id.editText);
Intent intent = new Intent();
intent.putExtra("a", et.getText().toString());
intent.putExtra("b", "bbbbbb");
intent.putExtra("c", "cccccc");
setResult(88,intent);
this.finish();
}
1. 跳转新的Activity方法
public void bt1(View view){
Toast.makeText(this, "bu1", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, v2.class);
startActivityForResult(intent, 99);
}
2. 回调方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String ss = data.getStringExtra("a").toString();
((EditText) findViewById(R.id.editText1)).setText(ss);
String b = data.getStringExtra("b").toString();
String c = data.getStringExtra("c").toString();
Toast.makeText(this, b, Toast.LENGTH_SHORT).show();
this.setTitle(c);
}
3. 从新的Activity返回值的的方法
public void bu2(View view){
EditText et = (EditText) findViewById(R.id.editText);
Intent intent = new Intent();
intent.putExtra("a", et.getText().toString());
intent.putExtra("b", "bbbbbb");
intent.putExtra("c", "cccccc");
setResult(88,intent);
this.finish();
}
2016年10月6日木曜日
添加按键,打开或隐藏Toolbar
添加按键,打开或隐藏Toolbar
private void toolbarButton() {
// 找到当前的Layout
ViewGroup thislayout = (ViewGroup) findViewById(R.id.activity_main);
// 新建按键
final Button button = new Button(this);
// 添加按键到Layout
thislayout.addView(button);
// 检查按键状态,并改变按键标题
if(SUPPORTBARCHK){
button.setText(R.string.heid);
}else{
button.setText(R.string.show);
}
// 按键事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 检查按键状态,并改变按键标题,并改变ActionBar的显示状态
if(SUPPORTBARCHK) {
// getActionBar().hide(); //错误
getSupportActionBar().hide();
button.setText(R.string.show);
SUPPORTBARCHK = false;
}else{
// getActionBar().show(); //错误
getSupportActionBar().show();
button.setText(R.string.heid);
SUPPORTBARCHK = true;
}
}
});
}
private void toolbarButton() {
// 找到当前的Layout
ViewGroup thislayout = (ViewGroup) findViewById(R.id.activity_main);
// 新建按键
final Button button = new Button(this);
// 添加按键到Layout
thislayout.addView(button);
// 检查按键状态,并改变按键标题
if(SUPPORTBARCHK){
button.setText(R.string.heid);
}else{
button.setText(R.string.show);
}
// 按键事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 检查按键状态,并改变按键标题,并改变ActionBar的显示状态
if(SUPPORTBARCHK) {
// getActionBar().hide(); //错误
getSupportActionBar().hide();
button.setText(R.string.show);
SUPPORTBARCHK = false;
}else{
// getActionBar().show(); //错误
getSupportActionBar().show();
button.setText(R.string.heid);
SUPPORTBARCHK = true;
}
}
});
}
2016年10月5日水曜日
EmptyActivite 基础上添加Toolbar
EmptyActivite 基础上添加Toolbar
MainActivity.java
import android.app.Activity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// EmptyActivite 基础上添加Toolbar
// step1,AppCompatActivity
// step2,noActionBar manifest
// step3,activitymain.xml
// step4, new Toolbar
Toolbar tb = (Toolbar) findViewById(R.id.my_toolbar);
tb.setTitle("newToolbar");
setSupportActionBar(tb);
// 背景图标 or 背景颜色
tb.setBackgroundResource(R.drawable.firefox);
tb.setBackgroundResource(android.R.color.holo_blue_bright);
// 添加先上导航,在ActionBar上添加
ActionBar ab =getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
// 测试
// ab.hide();
}
// 添加ToolBar 的选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mif = new MenuInflater(this);
mif.inflate(R.menu.optmenu, menu);
menu.add("addMenu1");
menu.add("addMenu2");
System.out.println(menu.size());
return super.onCreateOptionsMenu(menu);
}
// ToolBar 选项菜单事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("item");
System.out.println(item.getTitle());
System.out.println(item.getItemId());
System.out.println(item.getMenuInfo());
return super.onOptionsItemSelected(item);
}
}
MainActivity.java
import android.app.Activity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// EmptyActivite 基础上添加Toolbar
// step1,AppCompatActivity
// step2,noActionBar manifest
// step3,activitymain.xml
// step4, new Toolbar
Toolbar tb = (Toolbar) findViewById(R.id.my_toolbar);
tb.setTitle("newToolbar");
setSupportActionBar(tb);
// 背景图标 or 背景颜色
tb.setBackgroundResource(R.drawable.firefox);
tb.setBackgroundResource(android.R.color.holo_blue_bright);
// 添加先上导航,在ActionBar上添加
ActionBar ab =getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
// 测试
// ab.hide();
}
// 添加ToolBar 的选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mif = new MenuInflater(this);
mif.inflate(R.menu.optmenu, menu);
menu.add("addMenu1");
menu.add("addMenu2");
System.out.println(menu.size());
return super.onCreateOptionsMenu(menu);
}
// ToolBar 选项菜单事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("item");
System.out.println(item.getTitle());
System.out.println(item.getItemId());
System.out.println(item.getMenuInfo());
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.java.m1004c">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.java.m1004c.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
optmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu1"
android:icon="@android:drawable/ic_dialog_email"
android:title="menu1.title"
android:showAsAction="ifRoom"
/>
<item
android:id="@+id/menu2"
android:title="menu2.title"
android:showAsAction="always"
/>
</menu>
2016年10月2日日曜日
setLayoutParams Android
setLayoutParams Android
view.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
));
view.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
));
Android Intent
2016/10/02
Android Intent
启动 Activity:
要启动的Activity.class
Intent intent = new intent(this, Activity.class);
要传递的数据
intent.putExtra("key", "message);
启动开始
startActivity(intent);
在被启动的Activity.class内获取数据
获取Intent
Intent intent = getIntent();
获取数据
String message = intent.getStringExtra("key");
if(getIntent().getStringExtra("key2") != null) {
TextView tv = (TextView) findViewById(R.id.textView2);
String kk = getIntent().getStringExtra("key2");
tv.setText(kk);
}
Android Intent
启动 Activity:
要启动的Activity.class
Intent intent = new intent(this, Activity.class);
要传递的数据
intent.putExtra("key", "message);
启动开始
startActivity(intent);
在被启动的Activity.class内获取数据
获取Intent
Intent intent = getIntent();
获取数据
String message = intent.getStringExtra("key");
if(getIntent().getStringExtra("key2") != null) {
TextView tv = (TextView) findViewById(R.id.textView2);
String kk = getIntent().getStringExtra("key2");
tv.setText(kk);
}
2016年10月1日土曜日
widget のレイアウトの変更
widget のレイアウトの変更
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import static android.view.ViewGroup.*;
import static android.widget.LinearLayout.*;
import static com.example.e560.m1001a.R.id.wrap_content;
public class MainActivity extends AppCompatActivity {
public static boolean j = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
t1();
}
protected void t1(){
final LinearLayout ll = new LinearLayout(this);
final TextView tv1 = new TextView(this);
final TextView tv2 = new TextView(this);
final Button tb = new Button(this);
tv1.setBackgroundColor(Color.BLUE);
tv1.setText("" + Color.BLUE);
tv2.setBackgroundColor(Color.CYAN);
tv2.setText("" + Color.CYAN);
tb.setText("button");
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tb);
setContentView(ll);
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button)v;
if(j){
j = false;
ll.setOrientation(VERTICAL);
tv2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tv1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tb.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
}else{
j = true;
ll.setOrientation(HORIZONTAL);
tv2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tv1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tb.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
}
setContentView(ll);
}
});
}
}
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import static android.view.ViewGroup.*;
import static android.widget.LinearLayout.*;
import static com.example.e560.m1001a.R.id.wrap_content;
public class MainActivity extends AppCompatActivity {
public static boolean j = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
t1();
}
protected void t1(){
final LinearLayout ll = new LinearLayout(this);
final TextView tv1 = new TextView(this);
final TextView tv2 = new TextView(this);
final Button tb = new Button(this);
tv1.setBackgroundColor(Color.BLUE);
tv1.setText("" + Color.BLUE);
tv2.setBackgroundColor(Color.CYAN);
tv2.setText("" + Color.CYAN);
tb.setText("button");
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tb);
setContentView(ll);
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button)v;
if(j){
j = false;
ll.setOrientation(VERTICAL);
tv2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tv1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tb.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
}else{
j = true;
ll.setOrientation(HORIZONTAL);
tv2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tv1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tb.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
}
setContentView(ll);
}
});
}
}
登録:
投稿 (Atom)