gpt4 book ai didi

java - Android 应用程序,带按钮的动态 Activity 窗口

转载 作者:行者123 更新时间:2023-12-02 05:54:23 27 4
gpt4 key购买 nike

我有一个想法。

我正在尝试在 Android 应用程序中实现一项功能。我有一个编辑文本和一个按钮,如果我在编辑文本中输入 3然后按下我想要转到另一个有 3 个按钮的 Activity 的按钮。

如果我输入 10,我想转到另一个 Activity 来显示 10 个按钮。我知道如何进行这样的切换,但我不知道如何动态地进行,

我想要制作 0 到 10 之间的值,但我不想有 10 个 fragment 。

请给我解决方案来实现它..

谢谢..

到目前为止:

mainactivity.java:

 package com.example.instances_temperature;

import com.example.instances_temperature.Tabel;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


EditText instances;
Button ok;

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

ok = (Button) findViewById(R.id.ok);
instances = (EditText) findViewById(R.id.instances);

ok.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (instances.getText().toString().length() > 0)
{
int value = Integer.parseInt(instances.getText().toString());;
if(value > 10)
Toast.makeText(getApplicationContext(), "Enter a value between 0 and 10!", Toast.LENGTH_SHORT).show();
else
if(value<0)
Toast.makeText(getApplicationContext(), "Enter a value between 0 and 10!", Toast.LENGTH_SHORT).show();
else
if(value >= 0 && value <=10)
{
schimba(v);
}
}
else
{
Toast.makeText(getApplicationContext(), "Enter a value between 0 and 10!", Toast.LENGTH_SHORT).show();
}

}
});


if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void schimba(View view){
int value = Integer.parseInt(instances.getText().toString());;
Intent intent = new Intent(this, Tabel.class);
intent.putExtra("max", value);
startActivity(intent);
}

}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.instances_temperature.MainActivity"
tools:ignore="MergeRootFrame" >

<TextView
android:id="@+id/ShowValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="139dp"
android:text="Instances No.:"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/instances"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="number" >

<requestFocus />
</EditText>

<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/instances"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="Ok"
android:onClick="schimba" />

</RelativeLayout>

tabel.java:

    package com.example.instances_temperature;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.Build;

public class Tabel extends ActionBarActivity {

int i;

int value;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabel);
Intent intentObject = getIntent();
value = intentObject.getIntExtra("max", 0);
LinearLayout layout = (LinearLayout)findViewById(R.id.container);

for(i=1;i<=value;i++)
{
LayoutInflater layoutinflate = null;
layoutinflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowview = layoutinflate.inflate( R.layout.ShowMe, null);

TextView showvalue;
showvalue = (TextView) rowview.findViewById(R.id.showid);
showvalue.setText(""+value);

layout.addView(rowview);
}



//showvalue.setText(String.valueOf(getIntent().getExtras().getInt("max")));


if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tabel, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

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

}

activity_tabel.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.instances_temperature.Tabel"
tools:ignore="MergeRootFrame" >

</LinearLayout>

inflation_layout.xml的LinearLayout代码

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

<TextView
android:id="@+id/ShowMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

LogCat 错误:

04-24 06:21:04.244: E/AndroidRuntime(269): FATAL EXCEPTION: main
04-24 06:21:04.244: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.instances_temperature/com.example.instances_temperature.Tabel}: java.lang.NullPointerException
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.os.Looper.loop(Looper.java:123)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 06:21:04.244: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 06:21:04.244: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 06:21:04.244: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 06:21:04.244: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 06:21:04.244: E/AndroidRuntime(269): at dalvik.system.NativeStart.main(Native Method)
04-24 06:21:04.244: E/AndroidRuntime(269): Caused by: java.lang.NullPointerException
04-24 06:21:04.244: E/AndroidRuntime(269): at com.example.instances_temperature.Tabel.onCreate(Tabel.java:42)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-24 06:21:04.244: E/AndroidRuntime(269): ... 11 more

最佳答案

您可以通过以下方式从 editText 获取值

String value= EditText.getText().toString();

通过Intent将值传递给另一个 Activity (Activity2)

现在在 Activity2 中,您在 onCreate() 中编写此代码

for(int i=1 ; i<= value ; i++){
Button b = new Button(getApplicationContext());
LinearLayout.addView(b);
}

关于java - Android 应用程序,带按钮的动态 Activity 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23237398/

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