/ / jak dodać fragment w pustym działaniu stworzonym w Androidzie - android, android-fragmenty, android-studio

jak dodać fragment w pustym działaniu stworzonym w Androidzie - android, android-fragmenty, android-studio

Próbuję dodać fragment w mojej głównej działalności, które stworzyłem jako puste działanie.I rzeczywiście chce uruchomić moduł Zxing przez kliknięcie przycisku z mojej aplikacji.

Usunąłem 3 linie kodu z metody oncreate i wstawiłem ją do metody onClick klasy PlaceholderFragment.

ale fragment nie działa poprawnie.

Nie mogłem zrozumieć w jakim pliku xml Jaki kod dodać?

oto mój kod:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#bcff9420">

<TextView android:text="@string/WelcomeStr" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtWelcome"
style="@style/Base.TextAppearance.AppCompat.Title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scanStr"
android:id="@+id/btnScan"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="examples.skan.zxing_demo" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

</application>

</manifest>

MainActivity.java

package examples.skan.zxing_demo;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
public static class PlaceholderFragment extends Fragment implements View.OnClickListener{
View rootView;

public PlaceholderFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.fragment_main,container,false);
return rootView;
}

@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnScan:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
break;
}
}
}
}

Odpowiedzi:

3 dla odpowiedzi № 1

Twoje pytanie ma wiele odpowiedzi. Ale najpierw przeczytaj samouczek fragmentu androida tutaj

wtedy zrozumiesz, jak używać fragmentów i do czego one służą. Nie próbuj edytować przykładowych kodów, zanim zrozumiesz niektóre podstawowe zasady Androida.


0 dla odpowiedzi nr 2

pusta aktywność XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Aktywność Java.

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frameLayout, new SettingsFragment(), "fragmentTag")
.disallowAddToBackStack()
.commit();
}

}