2018年4月21日土曜日

為Fragment添加一個接口,是能在MainActivity下找到Fragment下的子控件View

為Fragment添加一個接口,是能在MainActivity下找到Fragment下的子控件View

public class F1 extends Fragment {
    private View view;
    public Find_button find_button;

    public void getFind_button(Find_button find_button) {
        this.find_button = find_button;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.f1, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        find_button.button(view);
    }

}

interface Find_button {
    void button(View view);
}


-----------------------------------
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addf1();
    }

    protected void addf1() {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        F1 f1 = new F1();
        fragmentTransaction.add(R.id.f1, f1, "f1");
        fragmentTransaction.commit();
        f1.getFind_button(new Find_button() {
            @Override
            public void button(View view) {
                final Button button = view.findViewById(R.id.button);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        button.setText("F1.Button");
                        Toast.makeText(MainActivity.this, "F1.Button", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}




2018年4月5日木曜日

Creating a Custom Toast View

Creating a Custom Toast View
创建自定义Toast视图

 LayoutInflater layoutInflater = getLayoutInflater();
 View view = layoutInflater.inflate(R.layout.toastlayout ,null);
 TextView textView = view.findViewById(R.id.tx);
 textView.setText("Creating a Custom Toast View");
 Toast toast = new Toast(getContext());
 toast.setView(view);
 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
 toast.setDuration(Toast.LENGTH_LONG);
 toast.show();



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@android:drawable/btn_dialog" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tx"/>
</LinearLayout>