gpt4 book ai didi

java - 第一个 Activity 仅适用于我的 Android 项目

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:22 24 4
gpt4 key购买 nike

我是 android 和 java 编程新手,所以我尝试从做几个项目中学习,但我遇到了这个问题。

当我尝试运行应用程序时,只有第一个 Activity 正在运行,而第二个 Activity 则不起作用。

我在哪里错过了这个应用程序?

我很困惑,两个 java 项目正在运行,但只有第一个 Activity 有效。

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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android_examples.wallpaper_android_examplescom.MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/img76"
android:layout_above="@+id/btn_right"
android:layout_alignParentTop="true"
android:layout_marginBottom="30dp"
android:scaleType="fitXY" />

<Button
android:id="@+id/btn_right"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_left"
android:layout_alignBottom="@+id/btn_left"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="42dp"
android:layout_marginRight="42dp"
android:text="RIGHT" />

<Button
android:id="@+id/btn_left"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="43dp"
android:layout_marginLeft="43dp"
android:layout_marginBottom="100dp"
android:text="LEFT" />


<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:text="Click here to Set this image as Wallpaper in android programmatically" />

</RelativeLayout>

MainActivity.java

package com.android_examples.wallpaper_android_examplescom;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

Button button;
ImageView imageView;
WallpaperManager wallpaperManager ;
Bitmap bitmap1, bitmap2 ;
DisplayMetrics displayMetrics ;
int width, height;
BitmapDrawable bitmapDrawable ;

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

button = (Button)findViewById(R.id.button);

imageView = (ImageView)findViewById(R.id.imageView);

wallpaperManager = WallpaperManager.getInstance(getApplicationContext());

bitmapDrawable = (BitmapDrawable) imageView.getDrawable();

bitmap1 = bitmapDrawable.getBitmap();

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

GetScreenWidthHeight();

SetBitmapSize();

wallpaperManager = WallpaperManager.getInstance(MainActivity.this);

try {

wallpaperManager.setBitmap(bitmap2);

wallpaperManager.suggestDesiredDimensions(width, height);


} catch (IOException e) {
e.printStackTrace();
}
}
});
}

public void GetScreenWidthHeight(){

displayMetrics = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

width = displayMetrics.widthPixels;

height = displayMetrics.heightPixels;

}

public void SetBitmapSize(){

bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);

}
}

login.java

package com.android_examples.wallpaper_android_examplescom;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class login extends AppCompatActivity {
private ImageView imageView;
private Button btn_right, btn_left;
private int current_image_index;
private int[] images = {R.drawable.img76, R.drawable.img2};

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

DisplayImage();
SwitchButton();
}

void DisplayImage(){
imageView = (ImageView)findViewById(R.id.imageView);
}

void SwitchButton(){
btn_right = (Button)findViewById(R.id.btn_right);
btn_left = (Button)findViewById(R.id.btn_left);
btn_right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imageView.setImageResource(images[current_image_index]);
}
});

btn_left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
current_image_index--;

if(current_image_index < 0){
current_image_index = images.length - 1;
}
imageView.setImageResource(images[current_image_index]);
}
}





);



}
}

Androidmanifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android_examples.wallpaper_android_examplescom">

<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>

</application>
</manifest>

最佳答案

无论您想在哪里调用下一个 Activity ,都可以使用以下命令:-

  Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

关于java - 第一个 Activity 仅适用于我的 Android 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55708296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com