我想创建一个字符串,只要按下ImageView就会附加其组件。
我有一个名为imageView的ImageView,并在点击时命名为ImageView,我将一个值附加到字符串。 最后,当点击一个按钮时,我希望字符串显示在最后一次覆盖中被叫出的Toast。 因此,我已经定义了字符串(命名结果),按钮和图像,但我仍然会遇到很多错误,最值得注意的是我无法回想起" value1"附加字符串时。
有什么想法吗?
Button button;
Button Click = (Button) findViewById(R.id.button);
String result;
ImageView Imageview = (ImageView) findViewById(R.id.imageView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
(...)
public class Program {
public void main(String[] args) {
final int value1 = 300;
double value2 = 3.14;
short value3 = 5;
char value4 = 'A';
// Create StringBuilder and add four values to it.
final StringBuilder builder = new StringBuilder();
builder.append(value2).append("\n");
builder.append(value3).append("\n");
builder.append(value4);
// Display results.
String result = builder.toString();
System.out.println(result);
public void onClick(View v) {
Imageview.setOnClickListener(new View.OnClickListener() {
builder.append(value1).append("\n");
}
}
}
@Override
public void onClick(View V){
Toast.makeText(getApplicationContext(),getApplicationContext().getResources().getString(Integer.parseInt(result)), Toast.LENGTH_LONG).show();
}
答案 0 :(得分:1)
此行的第一个错误
ImageView Imageview = (ImageView) findViewById(R.id.imageView);
你不能在你的类块中使用findById你必须在像onCreate这样的函数中使用它,所以改变你的代码....你的代码必须 像这样的东西
ImageView imageView;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
imageView= (ImageView) findViewById(R.id.imageView);
textView=(TextView) findViewById(R.id.tv_show);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String test=textView.getText().toString();
textView.setText(test);
}
});
}
记住你必须在你的xml文件中添加一个textView并将这个id设置为tv_show;
答案 1 :(得分:0)
让def main_menu():
print ("\n Main Menu ")
print ("1.Average Score of class = 'avg'")
main_menu()
option = input("option [avg]: ")
option_class = input("class: ")
one = "1.txt"
two = "2.txt"
three = "3.txt"
if option.lower() == 'avg' and option_class == '1':
with open(one) as f:
the_list = [int(l.strip().split()[-1]) for l in f]
b = sum(the_list)
length = len(the_list)
avg = float(b) / length if length else 0
print ("Average of Class is: ", avg)
if option.lower() == 'avg' and option_class == '2':
with open(two) as f:
the_list = [int(l.strip().split()[-1]) for l in f]
b = sum(the_list)
length = len(the_list)
avg = float(b) / length if length else 0
print ("Average of Class is: ", avg)
if option.lower() == 'avg' and option_class == '3':
with open(three) as f:
the_list = [int(l.strip().split()[-1]) for l in f]
b = sum(the_list)
length = len(the_list)
avg = float(b) / length if length else 0
print ("Average of Class is: ", avg)
然后TextView
:
append();
中的:
XML
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:text="" />
中的:
Java
答案 2 :(得分:0)
您可以点击按钮
添加这样的字符串String stringName = "";
Imageview.setOnClickListener(new View.OnClickListener() {
strigName = stringName + "New appended data";
}
button.setOnClickListener(new View.OnClickListener() {
Toast.makeText(MainActivity.this, stringName, Toast.LENGTH_SHORT).show();
}
答案 3 :(得分:0)
您的主要活动Java类有类似这样的东西
public class MainActivity extends AppCompatActivity {
Button button;
Button click;
String result ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button) findViewById(R.id.button);
final int value1 = 300;
final double value2 = 3.14;
final short value3 = 5;
final char value4 = 'A';
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//create the send intent
Intent shareIntent =
new Intent(android.content.Intent.ACTION_SEND);
//set the type
shareIntent.setType("text/plain");
//add a subject
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Insert Subject Here");
//build the body of the message to be shared
String shareMessage = "Insert message body here.";
//add the message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
shareMessage);
//start the chooser for sharing
startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.FinalString)));
}
});
// Create StringBuilder and add four values to it.
final StringBuilder builder = new StringBuilder();
builder.append(value2).append("\n");
builder.append(value3).append("\n");
builder.append(value4);
ImageView imageview = (ImageView) findViewById(R.id.imageView);
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder.append(value1).append("\n");
}
});
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,builder.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
我将您的XMl文件更改为此更好
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:clickable="true"
android:src="@android:drawable/ic_input_add" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
并对您的错误there
显示评论